Lindent of core build. Drivers checked for no binary diffs. A few files
[platform/upstream/libdrm.git] / linux-core / drm_pci.c
1 /* drm_pci.h -- PCI DMA memory management wrappers for DRM -*- linux-c -*- */
2 /**
3  * \file drm_pci.h
4  * \brief Functions and ioctls to manage PCI memory
5  *
6  * \warning These interfaces aren't stable yet.
7  *
8  * \todo Implement the remaining ioctl's for the PCI pools.
9  * \todo Add support to map these buffers.
10  * \todo The wrappers here are so thin that they would be better off inlined..
11  *
12  * \author Jose Fonseca <jrfonseca@tungstengraphics.com>
13  * \author Leif Delgass <ldelgass@retinalburn.net>
14  */
15
16 /*
17  * Copyright 2003 Jos�Fonseca.
18  * Copyright 2003 Leif Delgass.
19  * All Rights Reserved.
20  *
21  * Permission is hereby granted, free of charge, to any person obtaining a
22  * copy of this software and associated documentation files (the "Software"),
23  * to deal in the Software without restriction, including without limitation
24  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
25  * and/or sell copies of the Software, and to permit persons to whom the
26  * Software is furnished to do so, subject to the following conditions:
27  *
28  * The above copyright notice and this permission notice (including the next
29  * paragraph) shall be included in all copies or substantial portions of the
30  * Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
35  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
36  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <linux/pci.h>
41 #include "drmP.h"
42
43 /**********************************************************************/
44 /** \name PCI memory */
45 /*@{*/
46
47 /**
48  * \brief Allocate a PCI consistent memory block, for DMA.
49  */
50 void *drm_pci_alloc(drm_device_t * dev, size_t size, size_t align,
51                     dma_addr_t maxaddr, dma_addr_t * busaddr)
52 {
53         void *address;
54 #if 0
55         unsigned long addr;
56         size_t sz;
57 #endif
58 #if DRM_DEBUG_MEMORY
59         int area = DRM_MEM_DMA;
60
61         spin_lock(&drm_mem_lock);
62         if ((drm_ram_used >> PAGE_SHIFT)
63             > (DRM_RAM_PERCENT * drm_ram_available) / 100) {
64                 spin_unlock(&drm_mem_lock);
65                 return 0;
66         }
67         spin_unlock(&drm_mem_lock);
68 #endif
69
70         /* pci_alloc_consistent only guarantees alignment to the smallest
71          * PAGE_SIZE order which is greater than or equal to the requested size.
72          * Return NULL here for now to make sure nobody tries for larger alignment
73          */
74         if (align > size)
75                 return NULL;
76
77         if (pci_set_dma_mask(dev->pdev, maxaddr) != 0) {
78                 DRM_ERROR("Setting pci dma mask failed\n");
79                 return NULL;
80         }
81
82         address = pci_alloc_consistent(dev->pdev, size, busaddr);
83
84 #if DRM_DEBUG_MEMORY
85         if (address == NULL) {
86                 spin_lock(&drm_mem_lock);
87                 ++drm_mem_stats[area].fail_count;
88                 spin_unlock(&drm_mem_lock);
89                 return NULL;
90         }
91
92         spin_lock(&drm_mem_lock);
93         ++drm_mem_stats[area].succeed_count;
94         drm_mem_stats[area].bytes_allocated += size;
95         drm_ram_used += size;
96         spin_unlock(&drm_mem_lock);
97 #else
98         if (address == NULL)
99                 return NULL;
100 #endif
101
102         memset(address, 0, size);
103
104 #if 0
105         /* XXX - Is virt_to_page() legal for consistent mem? */
106         /* Reserve */
107         for (addr = (unsigned long)address, sz = size;
108              sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
109                 SetPageReserved(virt_to_page(addr));
110         }
111 #endif
112
113         return address;
114 }
115 EXPORT_SYMBOL(drm_pci_alloc);
116
117 /**
118  * \brief Free a PCI consistent memory block.
119  */
120 void
121 drm_pci_free(drm_device_t * dev, size_t size, void *vaddr, dma_addr_t busaddr)
122 {
123 #if 0
124         unsigned long addr;
125         size_t sz;
126 #endif
127 #if DRM_DEBUG_MEMORY
128         int area = DRM_MEM_DMA;
129         int alloc_count;
130         int free_count;
131 #endif
132
133         if (!vaddr) {
134 #if DRM_DEBUG_MEMORY
135                 DRM_MEM_ERROR(area, "Attempt to free address 0\n");
136 #endif
137         } else {
138 #if 0
139                 /* XXX - Is virt_to_page() legal for consistent mem? */
140                 /* Unreserve */
141                 for (addr = (unsigned long)vaddr, sz = size;
142                      sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
143                         ClearPageReserved(virt_to_page(addr));
144                 }
145 #endif
146                 pci_free_consistent(dev->pdev, size, vaddr, busaddr);
147         }
148
149 #if DRM_DEBUG_MEMORY
150         spin_lock(&drm_mem_lock);
151         free_count = ++drm_mem_stats[area].free_count;
152         alloc_count = drm_mem_stats[area].succeed_count;
153         drm_mem_stats[area].bytes_freed += size;
154         drm_ram_used -= size;
155         spin_unlock(&drm_mem_lock);
156         if (free_count > alloc_count) {
157                 DRM_MEM_ERROR(area,
158                               "Excess frees: %d frees, %d allocs\n",
159                               free_count, alloc_count);
160         }
161 #endif
162
163 }
164 EXPORT_SYMBOL(drm_pci_free);
165
166 /*@}*/