0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #include <linux/mm.h>
0035 #include <linux/slab.h>
0036 #include <linux/vmalloc.h>
0037
0038 #include <drm/drm.h>
0039 #include <drm/drm_drv.h>
0040 #include <drm/drm_print.h>
0041
0042 #include "drm_legacy.h"
0043
0044 #define DEBUG_SCATTER 0
0045
0046 static void drm_sg_cleanup(struct drm_sg_mem * entry)
0047 {
0048 struct page *page;
0049 int i;
0050
0051 for (i = 0; i < entry->pages; i++) {
0052 page = entry->pagelist[i];
0053 if (page)
0054 ClearPageReserved(page);
0055 }
0056
0057 vfree(entry->virtual);
0058
0059 kfree(entry->busaddr);
0060 kfree(entry->pagelist);
0061 kfree(entry);
0062 }
0063
0064 void drm_legacy_sg_cleanup(struct drm_device *dev)
0065 {
0066 if (drm_core_check_feature(dev, DRIVER_SG) && dev->sg &&
0067 drm_core_check_feature(dev, DRIVER_LEGACY)) {
0068 drm_sg_cleanup(dev->sg);
0069 dev->sg = NULL;
0070 }
0071 }
0072 #ifdef _LP64
0073 # define ScatterHandle(x) (unsigned int)((x >> 32) + (x & ((1L << 32) - 1)))
0074 #else
0075 # define ScatterHandle(x) (unsigned int)(x)
0076 #endif
0077
0078 int drm_legacy_sg_alloc(struct drm_device *dev, void *data,
0079 struct drm_file *file_priv)
0080 {
0081 struct drm_scatter_gather *request = data;
0082 struct drm_sg_mem *entry;
0083 unsigned long pages, i, j;
0084
0085 DRM_DEBUG("\n");
0086
0087 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
0088 return -EOPNOTSUPP;
0089
0090 if (!drm_core_check_feature(dev, DRIVER_SG))
0091 return -EOPNOTSUPP;
0092
0093 if (request->size > SIZE_MAX - PAGE_SIZE)
0094 return -EINVAL;
0095
0096 if (dev->sg)
0097 return -EINVAL;
0098
0099 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
0100 if (!entry)
0101 return -ENOMEM;
0102
0103 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
0104 DRM_DEBUG("size=%ld pages=%ld\n", request->size, pages);
0105
0106 entry->pages = pages;
0107 entry->pagelist = kcalloc(pages, sizeof(*entry->pagelist), GFP_KERNEL);
0108 if (!entry->pagelist) {
0109 kfree(entry);
0110 return -ENOMEM;
0111 }
0112
0113 entry->busaddr = kcalloc(pages, sizeof(*entry->busaddr), GFP_KERNEL);
0114 if (!entry->busaddr) {
0115 kfree(entry->pagelist);
0116 kfree(entry);
0117 return -ENOMEM;
0118 }
0119
0120 entry->virtual = vmalloc_32(pages << PAGE_SHIFT);
0121 if (!entry->virtual) {
0122 kfree(entry->busaddr);
0123 kfree(entry->pagelist);
0124 kfree(entry);
0125 return -ENOMEM;
0126 }
0127
0128
0129
0130
0131 memset(entry->virtual, 0, pages << PAGE_SHIFT);
0132
0133 entry->handle = ScatterHandle((unsigned long)entry->virtual);
0134
0135 DRM_DEBUG("handle = %08lx\n", entry->handle);
0136 DRM_DEBUG("virtual = %p\n", entry->virtual);
0137
0138 for (i = (unsigned long)entry->virtual, j = 0; j < pages;
0139 i += PAGE_SIZE, j++) {
0140 entry->pagelist[j] = vmalloc_to_page((void *)i);
0141 if (!entry->pagelist[j])
0142 goto failed;
0143 SetPageReserved(entry->pagelist[j]);
0144 }
0145
0146 request->handle = entry->handle;
0147
0148 dev->sg = entry;
0149
0150 #if DEBUG_SCATTER
0151
0152
0153
0154 {
0155 int error = 0;
0156
0157 for (i = 0; i < pages; i++) {
0158 unsigned long *tmp;
0159
0160 tmp = page_address(entry->pagelist[i]);
0161 for (j = 0;
0162 j < PAGE_SIZE / sizeof(unsigned long);
0163 j++, tmp++) {
0164 *tmp = 0xcafebabe;
0165 }
0166 tmp = (unsigned long *)((u8 *) entry->virtual +
0167 (PAGE_SIZE * i));
0168 for (j = 0;
0169 j < PAGE_SIZE / sizeof(unsigned long);
0170 j++, tmp++) {
0171 if (*tmp != 0xcafebabe && error == 0) {
0172 error = 1;
0173 DRM_ERROR("Scatter allocation error, "
0174 "pagelist does not match "
0175 "virtual mapping\n");
0176 }
0177 }
0178 tmp = page_address(entry->pagelist[i]);
0179 for (j = 0;
0180 j < PAGE_SIZE / sizeof(unsigned long);
0181 j++, tmp++) {
0182 *tmp = 0;
0183 }
0184 }
0185 if (error == 0)
0186 DRM_ERROR("Scatter allocation matches pagelist\n");
0187 }
0188 #endif
0189
0190 return 0;
0191
0192 failed:
0193 drm_sg_cleanup(entry);
0194 return -ENOMEM;
0195 }
0196
0197 int drm_legacy_sg_free(struct drm_device *dev, void *data,
0198 struct drm_file *file_priv)
0199 {
0200 struct drm_scatter_gather *request = data;
0201 struct drm_sg_mem *entry;
0202
0203 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
0204 return -EOPNOTSUPP;
0205
0206 if (!drm_core_check_feature(dev, DRIVER_SG))
0207 return -EOPNOTSUPP;
0208
0209 entry = dev->sg;
0210 dev->sg = NULL;
0211
0212 if (!entry || entry->handle != request->handle)
0213 return -EINVAL;
0214
0215 DRM_DEBUG("virtual = %p\n", entry->virtual);
0216
0217 drm_sg_cleanup(entry);
0218
0219 return 0;
0220 }