forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-31 f70575805708cabdedea7498aaa3f710fde4d920
kernel/drivers/gpu/drm/drm_bufs.c
....@@ -28,20 +28,32 @@
2828 * OTHER DEALINGS IN THE SOFTWARE.
2929 */
3030
31
-#include <linux/vmalloc.h>
32
-#include <linux/slab.h>
33
-#include <linux/log2.h>
3431 #include <linux/export.h>
32
+#include <linux/log2.h>
33
+#include <linux/mm.h>
34
+#include <linux/mman.h>
35
+#include <linux/nospec.h>
36
+#include <linux/pci.h>
37
+#include <linux/slab.h>
38
+#include <linux/uaccess.h>
39
+#include <linux/vmalloc.h>
40
+
3541 #include <asm/shmparam.h>
36
-#include <drm/drmP.h>
42
+
43
+#include <drm/drm_agpsupport.h>
44
+#include <drm/drm_device.h>
45
+#include <drm/drm_drv.h>
46
+#include <drm/drm_file.h>
47
+#include <drm/drm_print.h>
48
+
3749 #include "drm_legacy.h"
3850
39
-#include <linux/nospec.h>
4051
4152 static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
4253 struct drm_local_map *map)
4354 {
4455 struct drm_map_list *entry;
56
+
4557 list_for_each_entry(entry, &dev->maplist, head) {
4658 /*
4759 * Because the kernel-userspace ABI is fixed at a 32-bit offset
....@@ -91,6 +103,7 @@
91103
92104 if (!use_hashed_handle) {
93105 int ret;
106
+
94107 hash->key = user_token >> PAGE_SHIFT;
95108 ret = drm_ht_insert_item(&dev->map_hash, hash);
96109 if (ret != -EINVAL)
....@@ -123,7 +136,7 @@
123136 shift, add);
124137 }
125138
126
-/**
139
+/*
127140 * Core function to create a range of memory available for mapping by a
128141 * non-root process.
129142 *
....@@ -138,7 +151,6 @@
138151 {
139152 struct drm_local_map *map;
140153 struct drm_map_list *list;
141
- drm_dma_handle_t *dmah;
142154 unsigned long user_token;
143155 int ret;
144156
....@@ -313,14 +325,14 @@
313325 * As we're limiting the address to 2^32-1 (or less),
314326 * casting it down to 32 bits is no problem, but we
315327 * need to point to a 64bit variable first. */
316
- dmah = drm_pci_alloc(dev, map->size, map->size);
317
- if (!dmah) {
328
+ map->handle = dma_alloc_coherent(&dev->pdev->dev,
329
+ map->size,
330
+ &map->offset,
331
+ GFP_KERNEL);
332
+ if (!map->handle) {
318333 kfree(map);
319334 return -ENOMEM;
320335 }
321
- map->handle = dmah->vaddr;
322
- map->offset = (unsigned long)dmah->busaddr;
323
- kfree(dmah);
324336 break;
325337 default:
326338 kfree(map);
....@@ -377,7 +389,19 @@
377389 }
378390 EXPORT_SYMBOL(drm_legacy_addmap);
379391
380
-/**
392
+struct drm_local_map *drm_legacy_findmap(struct drm_device *dev,
393
+ unsigned int token)
394
+{
395
+ struct drm_map_list *_entry;
396
+
397
+ list_for_each_entry(_entry, &dev->maplist, head)
398
+ if (_entry->user_token == token)
399
+ return _entry->map;
400
+ return NULL;
401
+}
402
+EXPORT_SYMBOL(drm_legacy_findmap);
403
+
404
+/*
381405 * Ioctl to specify a range of memory that is available for mapping by a
382406 * non-root process.
383407 *
....@@ -400,7 +424,7 @@
400424
401425 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
402426 !drm_core_check_feature(dev, DRIVER_LEGACY))
403
- return -EINVAL;
427
+ return -EOPNOTSUPP;
404428
405429 err = drm_addmap_core(dev, map->offset, map->size, map->type,
406430 map->flags, &maplist);
....@@ -446,7 +470,7 @@
446470
447471 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
448472 !drm_core_check_feature(dev, DRIVER_LEGACY))
449
- return -EINVAL;
473
+ return -EOPNOTSUPP;
450474
451475 idx = map->offset;
452476 if (idx < 0)
....@@ -478,12 +502,12 @@
478502 return 0;
479503 }
480504
481
-/**
505
+/*
482506 * Remove a map private from list and deallocate resources if the mapping
483507 * isn't in use.
484508 *
485509 * Searches the map on drm_device::maplist, removes it from the list, see if
486
- * its being used, and free any associate resource (such as MTRR's) if it's not
510
+ * it's being used, and free any associated resource (such as MTRR's) if it's not
487511 * being on use.
488512 *
489513 * \sa drm_legacy_addmap
....@@ -491,7 +515,6 @@
491515 int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
492516 {
493517 struct drm_map_list *r_list = NULL, *list_t;
494
- drm_dma_handle_t dmah;
495518 int found = 0;
496519 struct drm_master *master;
497520
....@@ -514,7 +537,7 @@
514537 switch (map->type) {
515538 case _DRM_REGISTERS:
516539 iounmap(map->handle);
517
- /* FALLTHROUGH */
540
+ fallthrough;
518541 case _DRM_FRAME_BUFFER:
519542 arch_phys_wc_del(map->mtrr);
520543 break;
....@@ -532,10 +555,10 @@
532555 case _DRM_SCATTER_GATHER:
533556 break;
534557 case _DRM_CONSISTENT:
535
- dmah.vaddr = map->handle;
536
- dmah.busaddr = map->offset;
537
- dmah.size = map->size;
538
- __drm_legacy_pci_free(dev, &dmah);
558
+ dma_free_coherent(&dev->pdev->dev,
559
+ map->size,
560
+ map->handle,
561
+ map->offset);
539562 break;
540563 }
541564 kfree(map);
....@@ -573,6 +596,14 @@
573596 mutex_unlock(&dev->struct_mutex);
574597 }
575598
599
+void drm_legacy_rmmaps(struct drm_device *dev)
600
+{
601
+ struct drm_map_list *r_list, *list_temp;
602
+
603
+ list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
604
+ drm_legacy_rmmap(dev, r_list->map);
605
+}
606
+
576607 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
577608 * the last close of the device, and this is necessary for cleanup when things
578609 * exit uncleanly. Therefore, having userland manually remove mappings seems
....@@ -598,7 +629,7 @@
598629
599630 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
600631 !drm_core_check_feature(dev, DRIVER_LEGACY))
601
- return -EINVAL;
632
+ return -EOPNOTSUPP;
602633
603634 mutex_lock(&dev->struct_mutex);
604635 list_for_each_entry(r_list, &dev->maplist, head) {
....@@ -610,7 +641,7 @@
610641 }
611642 }
612643
613
- /* List has wrapped around to the head pointer, or its empty we didn't
644
+ /* List has wrapped around to the head pointer, or it's empty we didn't
614645 * find anything.
615646 */
616647 if (list_empty(&dev->maplist) || !map) {
....@@ -631,7 +662,7 @@
631662 return ret;
632663 }
633664
634
-/**
665
+/*
635666 * Cleanup after an error on one of the addbufs() functions.
636667 *
637668 * \param dev DRM device.
....@@ -666,7 +697,7 @@
666697 }
667698
668699 #if IS_ENABLED(CONFIG_AGP)
669
-/**
700
+/*
670701 * Add AGP buffers for DMA transfers.
671702 *
672703 * \param dev struct drm_device to which the buffers are to be added.
....@@ -862,7 +893,7 @@
862893 struct drm_buf **temp_buflist;
863894
864895 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
865
- return -EINVAL;
896
+ return -EOPNOTSUPP;
866897
867898 if (!dma)
868899 return -EINVAL;
....@@ -1066,7 +1097,7 @@
10661097 struct drm_buf **temp_buflist;
10671098
10681099 if (!drm_core_check_feature(dev, DRIVER_SG))
1069
- return -EINVAL;
1100
+ return -EOPNOTSUPP;
10701101
10711102 if (!dma)
10721103 return -EINVAL;
....@@ -1202,7 +1233,7 @@
12021233 return 0;
12031234 }
12041235
1205
-/**
1236
+/*
12061237 * Add buffers for DMA transfers (ioctl).
12071238 *
12081239 * \param inode device inode.
....@@ -1223,10 +1254,10 @@
12231254 int ret;
12241255
12251256 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1226
- return -EINVAL;
1257
+ return -EOPNOTSUPP;
12271258
12281259 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1229
- return -EINVAL;
1260
+ return -EOPNOTSUPP;
12301261
12311262 #if IS_ENABLED(CONFIG_AGP)
12321263 if (request->flags & _DRM_AGP_BUFFER)
....@@ -1243,7 +1274,7 @@
12431274 return ret;
12441275 }
12451276
1246
-/**
1277
+/*
12471278 * Get information about the buffer mappings.
12481279 *
12491280 * This was originally mean for debugging purposes, or by a sophisticated
....@@ -1269,10 +1300,10 @@
12691300 int count;
12701301
12711302 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1272
- return -EINVAL;
1303
+ return -EOPNOTSUPP;
12731304
12741305 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1275
- return -EINVAL;
1306
+ return -EOPNOTSUPP;
12761307
12771308 if (!dma)
12781309 return -EINVAL;
....@@ -1295,6 +1326,7 @@
12951326 if (*p >= count) {
12961327 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
12971328 struct drm_buf_entry *from = &dma->bufs[i];
1329
+
12981330 if (from->buf_count) {
12991331 if (f(data, count, from) < 0)
13001332 return -EFAULT;
....@@ -1331,10 +1363,11 @@
13311363 struct drm_file *file_priv)
13321364 {
13331365 struct drm_buf_info *request = data;
1366
+
13341367 return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf);
13351368 }
13361369
1337
-/**
1370
+/*
13381371 * Specifies a low and high water mark for buffer allocation
13391372 *
13401373 * \param inode device inode.
....@@ -1357,10 +1390,10 @@
13571390 struct drm_buf_entry *entry;
13581391
13591392 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1360
- return -EINVAL;
1393
+ return -EOPNOTSUPP;
13611394
13621395 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1363
- return -EINVAL;
1396
+ return -EOPNOTSUPP;
13641397
13651398 if (!dma)
13661399 return -EINVAL;
....@@ -1383,7 +1416,7 @@
13831416 return 0;
13841417 }
13851418
1386
-/**
1419
+/*
13871420 * Unreserve the buffers in list, previously reserved using drmDMA.
13881421 *
13891422 * \param inode device inode.
....@@ -1405,10 +1438,10 @@
14051438 struct drm_buf *buf;
14061439
14071440 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1408
- return -EINVAL;
1441
+ return -EOPNOTSUPP;
14091442
14101443 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1411
- return -EINVAL;
1444
+ return -EOPNOTSUPP;
14121445
14131446 if (!dma)
14141447 return -EINVAL;
....@@ -1435,7 +1468,7 @@
14351468 return 0;
14361469 }
14371470
1438
-/**
1471
+/*
14391472 * Maps all of the DMA buffers into client-virtual space (ioctl).
14401473 *
14411474 * \param inode device inode.
....@@ -1461,10 +1494,10 @@
14611494 int i;
14621495
14631496 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1464
- return -EINVAL;
1497
+ return -EOPNOTSUPP;
14651498
14661499 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1467
- return -EINVAL;
1500
+ return -EOPNOTSUPP;
14681501
14691502 if (!dma)
14701503 return -EINVAL;
....@@ -1542,6 +1575,7 @@
15421575 struct drm_file *file_priv)
15431576 {
15441577 struct drm_buf_map *request = data;
1578
+
15451579 return __drm_legacy_mapbufs(dev, data, &request->count,
15461580 &request->virtual, map_one_buf,
15471581 file_priv);
....@@ -1551,7 +1585,7 @@
15511585 struct drm_file *file_priv)
15521586 {
15531587 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1554
- return -EINVAL;
1588
+ return -EOPNOTSUPP;
15551589
15561590 if (dev->driver->dma_ioctl)
15571591 return dev->driver->dma_ioctl(dev, data, file_priv);