vc4_bo.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Copyright © 2015 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. /**
  9. * DOC: VC4 GEM BO management support
  10. *
  11. * The VC4 GPU architecture (both scanout and rendering) has direct
  12. * access to system memory with no MMU in between. To support it, we
  13. * use the GEM CMA helper functions to allocate contiguous ranges of
  14. * physical memory for our BOs.
  15. *
  16. * Since the CMA allocator is very slow, we keep a cache of recently
  17. * freed BOs around so that the kernel's allocation of objects for 3D
  18. * rendering can return quickly.
  19. */
  20. #include <linux/dma-buf.h>
  21. #include "vc4_drv.h"
  22. #include "uapi/drm/vc4_drm.h"
  23. static const char * const bo_type_names[] = {
  24. "kernel",
  25. "V3D",
  26. "V3D shader",
  27. "dumb",
  28. "binner",
  29. "RCL",
  30. "BCL",
  31. "kernel BO cache",
  32. };
  33. static bool is_user_label(int label)
  34. {
  35. return label >= VC4_BO_TYPE_COUNT;
  36. }
  37. static void vc4_bo_stats_dump(struct vc4_dev *vc4)
  38. {
  39. int i;
  40. for (i = 0; i < vc4->num_labels; i++) {
  41. if (!vc4->bo_labels[i].num_allocated)
  42. continue;
  43. DRM_INFO("%30s: %6dkb BOs (%d)\n",
  44. vc4->bo_labels[i].name,
  45. vc4->bo_labels[i].size_allocated / 1024,
  46. vc4->bo_labels[i].num_allocated);
  47. }
  48. mutex_lock(&vc4->purgeable.lock);
  49. if (vc4->purgeable.num)
  50. DRM_INFO("%30s: %6zdkb BOs (%d)\n", "userspace BO cache",
  51. vc4->purgeable.size / 1024, vc4->purgeable.num);
  52. if (vc4->purgeable.purged_num)
  53. DRM_INFO("%30s: %6zdkb BOs (%d)\n", "total purged BO",
  54. vc4->purgeable.purged_size / 1024,
  55. vc4->purgeable.purged_num);
  56. mutex_unlock(&vc4->purgeable.lock);
  57. }
  58. #ifdef CONFIG_DEBUG_FS
  59. int vc4_bo_stats_debugfs(struct seq_file *m, void *unused)
  60. {
  61. struct drm_info_node *node = (struct drm_info_node *)m->private;
  62. struct drm_device *dev = node->minor->dev;
  63. struct vc4_dev *vc4 = to_vc4_dev(dev);
  64. int i;
  65. mutex_lock(&vc4->bo_lock);
  66. for (i = 0; i < vc4->num_labels; i++) {
  67. if (!vc4->bo_labels[i].num_allocated)
  68. continue;
  69. seq_printf(m, "%30s: %6dkb BOs (%d)\n",
  70. vc4->bo_labels[i].name,
  71. vc4->bo_labels[i].size_allocated / 1024,
  72. vc4->bo_labels[i].num_allocated);
  73. }
  74. mutex_unlock(&vc4->bo_lock);
  75. mutex_lock(&vc4->purgeable.lock);
  76. if (vc4->purgeable.num)
  77. seq_printf(m, "%30s: %6zdkb BOs (%d)\n", "userspace BO cache",
  78. vc4->purgeable.size / 1024, vc4->purgeable.num);
  79. if (vc4->purgeable.purged_num)
  80. seq_printf(m, "%30s: %6zdkb BOs (%d)\n", "total purged BO",
  81. vc4->purgeable.purged_size / 1024,
  82. vc4->purgeable.purged_num);
  83. mutex_unlock(&vc4->purgeable.lock);
  84. return 0;
  85. }
  86. #endif
  87. /* Takes ownership of *name and returns the appropriate slot for it in
  88. * the bo_labels[] array, extending it as necessary.
  89. *
  90. * This is inefficient and could use a hash table instead of walking
  91. * an array and strcmp()ing. However, the assumption is that user
  92. * labeling will be infrequent (scanout buffers and other long-lived
  93. * objects, or debug driver builds), so we can live with it for now.
  94. */
  95. static int vc4_get_user_label(struct vc4_dev *vc4, const char *name)
  96. {
  97. int i;
  98. int free_slot = -1;
  99. for (i = 0; i < vc4->num_labels; i++) {
  100. if (!vc4->bo_labels[i].name) {
  101. free_slot = i;
  102. } else if (strcmp(vc4->bo_labels[i].name, name) == 0) {
  103. kfree(name);
  104. return i;
  105. }
  106. }
  107. if (free_slot != -1) {
  108. WARN_ON(vc4->bo_labels[free_slot].num_allocated != 0);
  109. vc4->bo_labels[free_slot].name = name;
  110. return free_slot;
  111. } else {
  112. u32 new_label_count = vc4->num_labels + 1;
  113. struct vc4_label *new_labels =
  114. krealloc(vc4->bo_labels,
  115. new_label_count * sizeof(*new_labels),
  116. GFP_KERNEL);
  117. if (!new_labels) {
  118. kfree(name);
  119. return -1;
  120. }
  121. free_slot = vc4->num_labels;
  122. vc4->bo_labels = new_labels;
  123. vc4->num_labels = new_label_count;
  124. vc4->bo_labels[free_slot].name = name;
  125. vc4->bo_labels[free_slot].num_allocated = 0;
  126. vc4->bo_labels[free_slot].size_allocated = 0;
  127. return free_slot;
  128. }
  129. }
  130. static void vc4_bo_set_label(struct drm_gem_object *gem_obj, int label)
  131. {
  132. struct vc4_bo *bo = to_vc4_bo(gem_obj);
  133. struct vc4_dev *vc4 = to_vc4_dev(gem_obj->dev);
  134. lockdep_assert_held(&vc4->bo_lock);
  135. if (label != -1) {
  136. vc4->bo_labels[label].num_allocated++;
  137. vc4->bo_labels[label].size_allocated += gem_obj->size;
  138. }
  139. vc4->bo_labels[bo->label].num_allocated--;
  140. vc4->bo_labels[bo->label].size_allocated -= gem_obj->size;
  141. if (vc4->bo_labels[bo->label].num_allocated == 0 &&
  142. is_user_label(bo->label)) {
  143. /* Free user BO label slots on last unreference.
  144. * Slots are just where we track the stats for a given
  145. * name, and once a name is unused we can reuse that
  146. * slot.
  147. */
  148. kfree(vc4->bo_labels[bo->label].name);
  149. vc4->bo_labels[bo->label].name = NULL;
  150. }
  151. bo->label = label;
  152. }
  153. static uint32_t bo_page_index(size_t size)
  154. {
  155. return (size / PAGE_SIZE) - 1;
  156. }
  157. static void vc4_bo_destroy(struct vc4_bo *bo)
  158. {
  159. struct drm_gem_object *obj = &bo->base.base;
  160. struct vc4_dev *vc4 = to_vc4_dev(obj->dev);
  161. lockdep_assert_held(&vc4->bo_lock);
  162. vc4_bo_set_label(obj, -1);
  163. if (bo->validated_shader) {
  164. kfree(bo->validated_shader->texture_samples);
  165. kfree(bo->validated_shader);
  166. bo->validated_shader = NULL;
  167. }
  168. reservation_object_fini(&bo->_resv);
  169. drm_gem_cma_free_object(obj);
  170. }
  171. static void vc4_bo_remove_from_cache(struct vc4_bo *bo)
  172. {
  173. struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
  174. lockdep_assert_held(&vc4->bo_lock);
  175. list_del(&bo->unref_head);
  176. list_del(&bo->size_head);
  177. }
  178. static struct list_head *vc4_get_cache_list_for_size(struct drm_device *dev,
  179. size_t size)
  180. {
  181. struct vc4_dev *vc4 = to_vc4_dev(dev);
  182. uint32_t page_index = bo_page_index(size);
  183. if (vc4->bo_cache.size_list_size <= page_index) {
  184. uint32_t new_size = max(vc4->bo_cache.size_list_size * 2,
  185. page_index + 1);
  186. struct list_head *new_list;
  187. uint32_t i;
  188. new_list = kmalloc_array(new_size, sizeof(struct list_head),
  189. GFP_KERNEL);
  190. if (!new_list)
  191. return NULL;
  192. /* Rebase the old cached BO lists to their new list
  193. * head locations.
  194. */
  195. for (i = 0; i < vc4->bo_cache.size_list_size; i++) {
  196. struct list_head *old_list =
  197. &vc4->bo_cache.size_list[i];
  198. if (list_empty(old_list))
  199. INIT_LIST_HEAD(&new_list[i]);
  200. else
  201. list_replace(old_list, &new_list[i]);
  202. }
  203. /* And initialize the brand new BO list heads. */
  204. for (i = vc4->bo_cache.size_list_size; i < new_size; i++)
  205. INIT_LIST_HEAD(&new_list[i]);
  206. kfree(vc4->bo_cache.size_list);
  207. vc4->bo_cache.size_list = new_list;
  208. vc4->bo_cache.size_list_size = new_size;
  209. }
  210. return &vc4->bo_cache.size_list[page_index];
  211. }
  212. static void vc4_bo_cache_purge(struct drm_device *dev)
  213. {
  214. struct vc4_dev *vc4 = to_vc4_dev(dev);
  215. mutex_lock(&vc4->bo_lock);
  216. while (!list_empty(&vc4->bo_cache.time_list)) {
  217. struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
  218. struct vc4_bo, unref_head);
  219. vc4_bo_remove_from_cache(bo);
  220. vc4_bo_destroy(bo);
  221. }
  222. mutex_unlock(&vc4->bo_lock);
  223. }
  224. void vc4_bo_add_to_purgeable_pool(struct vc4_bo *bo)
  225. {
  226. struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
  227. mutex_lock(&vc4->purgeable.lock);
  228. list_add_tail(&bo->size_head, &vc4->purgeable.list);
  229. vc4->purgeable.num++;
  230. vc4->purgeable.size += bo->base.base.size;
  231. mutex_unlock(&vc4->purgeable.lock);
  232. }
  233. static void vc4_bo_remove_from_purgeable_pool_locked(struct vc4_bo *bo)
  234. {
  235. struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
  236. /* list_del_init() is used here because the caller might release
  237. * the purgeable lock in order to acquire the madv one and update the
  238. * madv status.
  239. * During this short period of time a user might decide to mark
  240. * the BO as unpurgeable, and if bo->madv is set to
  241. * VC4_MADV_DONTNEED it will try to remove the BO from the
  242. * purgeable list which will fail if the ->next/prev fields
  243. * are set to LIST_POISON1/LIST_POISON2 (which is what
  244. * list_del() does).
  245. * Re-initializing the list element guarantees that list_del()
  246. * will work correctly even if it's a NOP.
  247. */
  248. list_del_init(&bo->size_head);
  249. vc4->purgeable.num--;
  250. vc4->purgeable.size -= bo->base.base.size;
  251. }
  252. void vc4_bo_remove_from_purgeable_pool(struct vc4_bo *bo)
  253. {
  254. struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
  255. mutex_lock(&vc4->purgeable.lock);
  256. vc4_bo_remove_from_purgeable_pool_locked(bo);
  257. mutex_unlock(&vc4->purgeable.lock);
  258. }
  259. static void vc4_bo_purge(struct drm_gem_object *obj)
  260. {
  261. struct vc4_bo *bo = to_vc4_bo(obj);
  262. struct drm_device *dev = obj->dev;
  263. WARN_ON(!mutex_is_locked(&bo->madv_lock));
  264. WARN_ON(bo->madv != VC4_MADV_DONTNEED);
  265. drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
  266. dma_free_wc(dev->dev, obj->size, bo->base.vaddr, bo->base.paddr);
  267. bo->base.vaddr = NULL;
  268. bo->madv = __VC4_MADV_PURGED;
  269. }
  270. static void vc4_bo_userspace_cache_purge(struct drm_device *dev)
  271. {
  272. struct vc4_dev *vc4 = to_vc4_dev(dev);
  273. mutex_lock(&vc4->purgeable.lock);
  274. while (!list_empty(&vc4->purgeable.list)) {
  275. struct vc4_bo *bo = list_first_entry(&vc4->purgeable.list,
  276. struct vc4_bo, size_head);
  277. struct drm_gem_object *obj = &bo->base.base;
  278. size_t purged_size = 0;
  279. vc4_bo_remove_from_purgeable_pool_locked(bo);
  280. /* Release the purgeable lock while we're purging the BO so
  281. * that other people can continue inserting things in the
  282. * purgeable pool without having to wait for all BOs to be
  283. * purged.
  284. */
  285. mutex_unlock(&vc4->purgeable.lock);
  286. mutex_lock(&bo->madv_lock);
  287. /* Since we released the purgeable pool lock before acquiring
  288. * the BO madv one, the user may have marked the BO as WILLNEED
  289. * and re-used it in the meantime.
  290. * Before purging the BO we need to make sure
  291. * - it is still marked as DONTNEED
  292. * - it has not been re-inserted in the purgeable list
  293. * - it is not used by HW blocks
  294. * If one of these conditions is not met, just skip the entry.
  295. */
  296. if (bo->madv == VC4_MADV_DONTNEED &&
  297. list_empty(&bo->size_head) &&
  298. !refcount_read(&bo->usecnt)) {
  299. purged_size = bo->base.base.size;
  300. vc4_bo_purge(obj);
  301. }
  302. mutex_unlock(&bo->madv_lock);
  303. mutex_lock(&vc4->purgeable.lock);
  304. if (purged_size) {
  305. vc4->purgeable.purged_size += purged_size;
  306. vc4->purgeable.purged_num++;
  307. }
  308. }
  309. mutex_unlock(&vc4->purgeable.lock);
  310. }
  311. static struct vc4_bo *vc4_bo_get_from_cache(struct drm_device *dev,
  312. uint32_t size,
  313. enum vc4_kernel_bo_type type)
  314. {
  315. struct vc4_dev *vc4 = to_vc4_dev(dev);
  316. uint32_t page_index = bo_page_index(size);
  317. struct vc4_bo *bo = NULL;
  318. size = roundup(size, PAGE_SIZE);
  319. mutex_lock(&vc4->bo_lock);
  320. if (page_index >= vc4->bo_cache.size_list_size)
  321. goto out;
  322. if (list_empty(&vc4->bo_cache.size_list[page_index]))
  323. goto out;
  324. bo = list_first_entry(&vc4->bo_cache.size_list[page_index],
  325. struct vc4_bo, size_head);
  326. vc4_bo_remove_from_cache(bo);
  327. kref_init(&bo->base.base.refcount);
  328. out:
  329. if (bo)
  330. vc4_bo_set_label(&bo->base.base, type);
  331. mutex_unlock(&vc4->bo_lock);
  332. return bo;
  333. }
  334. /**
  335. * vc4_gem_create_object - Implementation of driver->gem_create_object.
  336. * @dev: DRM device
  337. * @size: Size in bytes of the memory the object will reference
  338. *
  339. * This lets the CMA helpers allocate object structs for us, and keep
  340. * our BO stats correct.
  341. */
  342. struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size)
  343. {
  344. struct vc4_dev *vc4 = to_vc4_dev(dev);
  345. struct vc4_bo *bo;
  346. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  347. if (!bo)
  348. return ERR_PTR(-ENOMEM);
  349. bo->madv = VC4_MADV_WILLNEED;
  350. refcount_set(&bo->usecnt, 0);
  351. mutex_init(&bo->madv_lock);
  352. mutex_lock(&vc4->bo_lock);
  353. bo->label = VC4_BO_TYPE_KERNEL;
  354. vc4->bo_labels[VC4_BO_TYPE_KERNEL].num_allocated++;
  355. vc4->bo_labels[VC4_BO_TYPE_KERNEL].size_allocated += size;
  356. mutex_unlock(&vc4->bo_lock);
  357. bo->resv = &bo->_resv;
  358. reservation_object_init(bo->resv);
  359. return &bo->base.base;
  360. }
  361. struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t unaligned_size,
  362. bool allow_unzeroed, enum vc4_kernel_bo_type type)
  363. {
  364. size_t size = roundup(unaligned_size, PAGE_SIZE);
  365. struct vc4_dev *vc4 = to_vc4_dev(dev);
  366. struct drm_gem_cma_object *cma_obj;
  367. struct vc4_bo *bo;
  368. if (size == 0)
  369. return ERR_PTR(-EINVAL);
  370. /* First, try to get a vc4_bo from the kernel BO cache. */
  371. bo = vc4_bo_get_from_cache(dev, size, type);
  372. if (bo) {
  373. if (!allow_unzeroed)
  374. memset(bo->base.vaddr, 0, bo->base.base.size);
  375. return bo;
  376. }
  377. cma_obj = drm_gem_cma_create(dev, size);
  378. if (IS_ERR(cma_obj)) {
  379. /*
  380. * If we've run out of CMA memory, kill the cache of
  381. * CMA allocations we've got laying around and try again.
  382. */
  383. vc4_bo_cache_purge(dev);
  384. cma_obj = drm_gem_cma_create(dev, size);
  385. }
  386. if (IS_ERR(cma_obj)) {
  387. /*
  388. * Still not enough CMA memory, purge the userspace BO
  389. * cache and retry.
  390. * This is sub-optimal since we purge the whole userspace
  391. * BO cache which forces user that want to re-use the BO to
  392. * restore its initial content.
  393. * Ideally, we should purge entries one by one and retry
  394. * after each to see if CMA allocation succeeds. Or even
  395. * better, try to find an entry with at least the same
  396. * size.
  397. */
  398. vc4_bo_userspace_cache_purge(dev);
  399. cma_obj = drm_gem_cma_create(dev, size);
  400. }
  401. if (IS_ERR(cma_obj)) {
  402. DRM_ERROR("Failed to allocate from CMA:\n");
  403. vc4_bo_stats_dump(vc4);
  404. return ERR_PTR(-ENOMEM);
  405. }
  406. bo = to_vc4_bo(&cma_obj->base);
  407. /* By default, BOs do not support the MADV ioctl. This will be enabled
  408. * only on BOs that are exposed to userspace (V3D, V3D_SHADER and DUMB
  409. * BOs).
  410. */
  411. bo->madv = __VC4_MADV_NOTSUPP;
  412. mutex_lock(&vc4->bo_lock);
  413. vc4_bo_set_label(&cma_obj->base, type);
  414. mutex_unlock(&vc4->bo_lock);
  415. return bo;
  416. }
  417. int vc4_dumb_create(struct drm_file *file_priv,
  418. struct drm_device *dev,
  419. struct drm_mode_create_dumb *args)
  420. {
  421. int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
  422. struct vc4_bo *bo = NULL;
  423. int ret;
  424. if (args->pitch < min_pitch)
  425. args->pitch = min_pitch;
  426. if (args->size < args->pitch * args->height)
  427. args->size = args->pitch * args->height;
  428. bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_DUMB);
  429. if (IS_ERR(bo))
  430. return PTR_ERR(bo);
  431. bo->madv = VC4_MADV_WILLNEED;
  432. ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
  433. drm_gem_object_put_unlocked(&bo->base.base);
  434. return ret;
  435. }
  436. static void vc4_bo_cache_free_old(struct drm_device *dev)
  437. {
  438. struct vc4_dev *vc4 = to_vc4_dev(dev);
  439. unsigned long expire_time = jiffies - msecs_to_jiffies(1000);
  440. lockdep_assert_held(&vc4->bo_lock);
  441. while (!list_empty(&vc4->bo_cache.time_list)) {
  442. struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
  443. struct vc4_bo, unref_head);
  444. if (time_before(expire_time, bo->free_time)) {
  445. mod_timer(&vc4->bo_cache.time_timer,
  446. round_jiffies_up(jiffies +
  447. msecs_to_jiffies(1000)));
  448. return;
  449. }
  450. vc4_bo_remove_from_cache(bo);
  451. vc4_bo_destroy(bo);
  452. }
  453. }
  454. /* Called on the last userspace/kernel unreference of the BO. Returns
  455. * it to the BO cache if possible, otherwise frees it.
  456. */
  457. void vc4_free_object(struct drm_gem_object *gem_bo)
  458. {
  459. struct drm_device *dev = gem_bo->dev;
  460. struct vc4_dev *vc4 = to_vc4_dev(dev);
  461. struct vc4_bo *bo = to_vc4_bo(gem_bo);
  462. struct list_head *cache_list;
  463. /* Remove the BO from the purgeable list. */
  464. mutex_lock(&bo->madv_lock);
  465. if (bo->madv == VC4_MADV_DONTNEED && !refcount_read(&bo->usecnt))
  466. vc4_bo_remove_from_purgeable_pool(bo);
  467. mutex_unlock(&bo->madv_lock);
  468. mutex_lock(&vc4->bo_lock);
  469. /* If the object references someone else's memory, we can't cache it.
  470. */
  471. if (gem_bo->import_attach) {
  472. vc4_bo_destroy(bo);
  473. goto out;
  474. }
  475. /* Don't cache if it was publicly named. */
  476. if (gem_bo->name) {
  477. vc4_bo_destroy(bo);
  478. goto out;
  479. }
  480. /* If this object was partially constructed but CMA allocation
  481. * had failed, just free it. Can also happen when the BO has been
  482. * purged.
  483. */
  484. if (!bo->base.vaddr) {
  485. vc4_bo_destroy(bo);
  486. goto out;
  487. }
  488. cache_list = vc4_get_cache_list_for_size(dev, gem_bo->size);
  489. if (!cache_list) {
  490. vc4_bo_destroy(bo);
  491. goto out;
  492. }
  493. if (bo->validated_shader) {
  494. kfree(bo->validated_shader->texture_samples);
  495. kfree(bo->validated_shader);
  496. bo->validated_shader = NULL;
  497. }
  498. /* Reset madv and usecnt before adding the BO to the cache. */
  499. bo->madv = __VC4_MADV_NOTSUPP;
  500. refcount_set(&bo->usecnt, 0);
  501. bo->t_format = false;
  502. bo->free_time = jiffies;
  503. list_add(&bo->size_head, cache_list);
  504. list_add(&bo->unref_head, &vc4->bo_cache.time_list);
  505. vc4_bo_set_label(&bo->base.base, VC4_BO_TYPE_KERNEL_CACHE);
  506. vc4_bo_cache_free_old(dev);
  507. out:
  508. mutex_unlock(&vc4->bo_lock);
  509. }
  510. static void vc4_bo_cache_time_work(struct work_struct *work)
  511. {
  512. struct vc4_dev *vc4 =
  513. container_of(work, struct vc4_dev, bo_cache.time_work);
  514. struct drm_device *dev = vc4->dev;
  515. mutex_lock(&vc4->bo_lock);
  516. vc4_bo_cache_free_old(dev);
  517. mutex_unlock(&vc4->bo_lock);
  518. }
  519. int vc4_bo_inc_usecnt(struct vc4_bo *bo)
  520. {
  521. int ret;
  522. /* Fast path: if the BO is already retained by someone, no need to
  523. * check the madv status.
  524. */
  525. if (refcount_inc_not_zero(&bo->usecnt))
  526. return 0;
  527. mutex_lock(&bo->madv_lock);
  528. switch (bo->madv) {
  529. case VC4_MADV_WILLNEED:
  530. if (!refcount_inc_not_zero(&bo->usecnt))
  531. refcount_set(&bo->usecnt, 1);
  532. ret = 0;
  533. break;
  534. case VC4_MADV_DONTNEED:
  535. /* We shouldn't use a BO marked as purgeable if at least
  536. * someone else retained its content by incrementing usecnt.
  537. * Luckily the BO hasn't been purged yet, but something wrong
  538. * is happening here. Just throw an error instead of
  539. * authorizing this use case.
  540. */
  541. case __VC4_MADV_PURGED:
  542. /* We can't use a purged BO. */
  543. default:
  544. /* Invalid madv value. */
  545. ret = -EINVAL;
  546. break;
  547. }
  548. mutex_unlock(&bo->madv_lock);
  549. return ret;
  550. }
  551. void vc4_bo_dec_usecnt(struct vc4_bo *bo)
  552. {
  553. /* Fast path: if the BO is still retained by someone, no need to test
  554. * the madv value.
  555. */
  556. if (refcount_dec_not_one(&bo->usecnt))
  557. return;
  558. mutex_lock(&bo->madv_lock);
  559. if (refcount_dec_and_test(&bo->usecnt) &&
  560. bo->madv == VC4_MADV_DONTNEED)
  561. vc4_bo_add_to_purgeable_pool(bo);
  562. mutex_unlock(&bo->madv_lock);
  563. }
  564. static void vc4_bo_cache_time_timer(struct timer_list *t)
  565. {
  566. struct vc4_dev *vc4 = from_timer(vc4, t, bo_cache.time_timer);
  567. schedule_work(&vc4->bo_cache.time_work);
  568. }
  569. struct reservation_object *vc4_prime_res_obj(struct drm_gem_object *obj)
  570. {
  571. struct vc4_bo *bo = to_vc4_bo(obj);
  572. return bo->resv;
  573. }
  574. struct dma_buf *
  575. vc4_prime_export(struct drm_device *dev, struct drm_gem_object *obj, int flags)
  576. {
  577. struct vc4_bo *bo = to_vc4_bo(obj);
  578. struct dma_buf *dmabuf;
  579. int ret;
  580. if (bo->validated_shader) {
  581. DRM_DEBUG("Attempting to export shader BO\n");
  582. return ERR_PTR(-EINVAL);
  583. }
  584. /* Note: as soon as the BO is exported it becomes unpurgeable, because
  585. * noone ever decrements the usecnt even if the reference held by the
  586. * exported BO is released. This shouldn't be a problem since we don't
  587. * expect exported BOs to be marked as purgeable.
  588. */
  589. ret = vc4_bo_inc_usecnt(bo);
  590. if (ret) {
  591. DRM_ERROR("Failed to increment BO usecnt\n");
  592. return ERR_PTR(ret);
  593. }
  594. dmabuf = drm_gem_prime_export(dev, obj, flags);
  595. if (IS_ERR(dmabuf))
  596. vc4_bo_dec_usecnt(bo);
  597. return dmabuf;
  598. }
  599. int vc4_fault(struct vm_fault *vmf)
  600. {
  601. struct vm_area_struct *vma = vmf->vma;
  602. struct drm_gem_object *obj = vma->vm_private_data;
  603. struct vc4_bo *bo = to_vc4_bo(obj);
  604. /* The only reason we would end up here is when user-space accesses
  605. * BO's memory after it's been purged.
  606. */
  607. mutex_lock(&bo->madv_lock);
  608. WARN_ON(bo->madv != __VC4_MADV_PURGED);
  609. mutex_unlock(&bo->madv_lock);
  610. return VM_FAULT_SIGBUS;
  611. }
  612. int vc4_mmap(struct file *filp, struct vm_area_struct *vma)
  613. {
  614. struct drm_gem_object *gem_obj;
  615. unsigned long vm_pgoff;
  616. struct vc4_bo *bo;
  617. int ret;
  618. ret = drm_gem_mmap(filp, vma);
  619. if (ret)
  620. return ret;
  621. gem_obj = vma->vm_private_data;
  622. bo = to_vc4_bo(gem_obj);
  623. if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
  624. DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
  625. return -EINVAL;
  626. }
  627. if (bo->madv != VC4_MADV_WILLNEED) {
  628. DRM_DEBUG("mmaping of %s BO not allowed\n",
  629. bo->madv == VC4_MADV_DONTNEED ?
  630. "purgeable" : "purged");
  631. return -EINVAL;
  632. }
  633. /*
  634. * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
  635. * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
  636. * the whole buffer.
  637. */
  638. vma->vm_flags &= ~VM_PFNMAP;
  639. /* This ->vm_pgoff dance is needed to make all parties happy:
  640. * - dma_mmap_wc() uses ->vm_pgoff as an offset within the allocated
  641. * mem-region, hence the need to set it to zero (the value set by
  642. * the DRM core is a virtual offset encoding the GEM object-id)
  643. * - the mmap() core logic needs ->vm_pgoff to be restored to its
  644. * initial value before returning from this function because it
  645. * encodes the offset of this GEM in the dev->anon_inode pseudo-file
  646. * and this information will be used when we invalidate userspace
  647. * mappings with drm_vma_node_unmap() (called from vc4_gem_purge()).
  648. */
  649. vm_pgoff = vma->vm_pgoff;
  650. vma->vm_pgoff = 0;
  651. ret = dma_mmap_wc(bo->base.base.dev->dev, vma, bo->base.vaddr,
  652. bo->base.paddr, vma->vm_end - vma->vm_start);
  653. vma->vm_pgoff = vm_pgoff;
  654. if (ret)
  655. drm_gem_vm_close(vma);
  656. return ret;
  657. }
  658. int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
  659. {
  660. struct vc4_bo *bo = to_vc4_bo(obj);
  661. if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
  662. DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
  663. return -EINVAL;
  664. }
  665. return drm_gem_cma_prime_mmap(obj, vma);
  666. }
  667. void *vc4_prime_vmap(struct drm_gem_object *obj)
  668. {
  669. struct vc4_bo *bo = to_vc4_bo(obj);
  670. if (bo->validated_shader) {
  671. DRM_DEBUG("mmaping of shader BOs not allowed.\n");
  672. return ERR_PTR(-EINVAL);
  673. }
  674. return drm_gem_cma_prime_vmap(obj);
  675. }
  676. struct drm_gem_object *
  677. vc4_prime_import_sg_table(struct drm_device *dev,
  678. struct dma_buf_attachment *attach,
  679. struct sg_table *sgt)
  680. {
  681. struct drm_gem_object *obj;
  682. struct vc4_bo *bo;
  683. obj = drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
  684. if (IS_ERR(obj))
  685. return obj;
  686. bo = to_vc4_bo(obj);
  687. bo->resv = attach->dmabuf->resv;
  688. return obj;
  689. }
  690. int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
  691. struct drm_file *file_priv)
  692. {
  693. struct drm_vc4_create_bo *args = data;
  694. struct vc4_bo *bo = NULL;
  695. int ret;
  696. /*
  697. * We can't allocate from the BO cache, because the BOs don't
  698. * get zeroed, and that might leak data between users.
  699. */
  700. bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_V3D);
  701. if (IS_ERR(bo))
  702. return PTR_ERR(bo);
  703. bo->madv = VC4_MADV_WILLNEED;
  704. ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
  705. drm_gem_object_put_unlocked(&bo->base.base);
  706. return ret;
  707. }
  708. int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
  709. struct drm_file *file_priv)
  710. {
  711. struct drm_vc4_mmap_bo *args = data;
  712. struct drm_gem_object *gem_obj;
  713. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  714. if (!gem_obj) {
  715. DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
  716. return -EINVAL;
  717. }
  718. /* The mmap offset was set up at BO allocation time. */
  719. args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
  720. drm_gem_object_put_unlocked(gem_obj);
  721. return 0;
  722. }
  723. int
  724. vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
  725. struct drm_file *file_priv)
  726. {
  727. struct drm_vc4_create_shader_bo *args = data;
  728. struct vc4_bo *bo = NULL;
  729. int ret;
  730. if (args->size == 0)
  731. return -EINVAL;
  732. if (args->size % sizeof(u64) != 0)
  733. return -EINVAL;
  734. if (args->flags != 0) {
  735. DRM_INFO("Unknown flags set: 0x%08x\n", args->flags);
  736. return -EINVAL;
  737. }
  738. if (args->pad != 0) {
  739. DRM_INFO("Pad set: 0x%08x\n", args->pad);
  740. return -EINVAL;
  741. }
  742. bo = vc4_bo_create(dev, args->size, true, VC4_BO_TYPE_V3D_SHADER);
  743. if (IS_ERR(bo))
  744. return PTR_ERR(bo);
  745. bo->madv = VC4_MADV_WILLNEED;
  746. if (copy_from_user(bo->base.vaddr,
  747. (void __user *)(uintptr_t)args->data,
  748. args->size)) {
  749. ret = -EFAULT;
  750. goto fail;
  751. }
  752. /* Clear the rest of the memory from allocating from the BO
  753. * cache.
  754. */
  755. memset(bo->base.vaddr + args->size, 0,
  756. bo->base.base.size - args->size);
  757. bo->validated_shader = vc4_validate_shader(&bo->base);
  758. if (!bo->validated_shader) {
  759. ret = -EINVAL;
  760. goto fail;
  761. }
  762. /* We have to create the handle after validation, to avoid
  763. * races for users to do doing things like mmap the shader BO.
  764. */
  765. ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
  766. fail:
  767. drm_gem_object_put_unlocked(&bo->base.base);
  768. return ret;
  769. }
  770. /**
  771. * vc4_set_tiling_ioctl() - Sets the tiling modifier for a BO.
  772. * @dev: DRM device
  773. * @data: ioctl argument
  774. * @file_priv: DRM file for this fd
  775. *
  776. * The tiling state of the BO decides the default modifier of an fb if
  777. * no specific modifier was set by userspace, and the return value of
  778. * vc4_get_tiling_ioctl() (so that userspace can treat a BO it
  779. * received from dmabuf as the same tiling format as the producer
  780. * used).
  781. */
  782. int vc4_set_tiling_ioctl(struct drm_device *dev, void *data,
  783. struct drm_file *file_priv)
  784. {
  785. struct drm_vc4_set_tiling *args = data;
  786. struct drm_gem_object *gem_obj;
  787. struct vc4_bo *bo;
  788. bool t_format;
  789. if (args->flags != 0)
  790. return -EINVAL;
  791. switch (args->modifier) {
  792. case DRM_FORMAT_MOD_NONE:
  793. t_format = false;
  794. break;
  795. case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
  796. t_format = true;
  797. break;
  798. default:
  799. return -EINVAL;
  800. }
  801. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  802. if (!gem_obj) {
  803. DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
  804. return -ENOENT;
  805. }
  806. bo = to_vc4_bo(gem_obj);
  807. bo->t_format = t_format;
  808. drm_gem_object_put_unlocked(gem_obj);
  809. return 0;
  810. }
  811. /**
  812. * vc4_get_tiling_ioctl() - Gets the tiling modifier for a BO.
  813. * @dev: DRM device
  814. * @data: ioctl argument
  815. * @file_priv: DRM file for this fd
  816. *
  817. * Returns the tiling modifier for a BO as set by vc4_set_tiling_ioctl().
  818. */
  819. int vc4_get_tiling_ioctl(struct drm_device *dev, void *data,
  820. struct drm_file *file_priv)
  821. {
  822. struct drm_vc4_get_tiling *args = data;
  823. struct drm_gem_object *gem_obj;
  824. struct vc4_bo *bo;
  825. if (args->flags != 0 || args->modifier != 0)
  826. return -EINVAL;
  827. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  828. if (!gem_obj) {
  829. DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
  830. return -ENOENT;
  831. }
  832. bo = to_vc4_bo(gem_obj);
  833. if (bo->t_format)
  834. args->modifier = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
  835. else
  836. args->modifier = DRM_FORMAT_MOD_NONE;
  837. drm_gem_object_put_unlocked(gem_obj);
  838. return 0;
  839. }
  840. int vc4_bo_cache_init(struct drm_device *dev)
  841. {
  842. struct vc4_dev *vc4 = to_vc4_dev(dev);
  843. int i;
  844. /* Create the initial set of BO labels that the kernel will
  845. * use. This lets us avoid a bunch of string reallocation in
  846. * the kernel's draw and BO allocation paths.
  847. */
  848. vc4->bo_labels = kcalloc(VC4_BO_TYPE_COUNT, sizeof(*vc4->bo_labels),
  849. GFP_KERNEL);
  850. if (!vc4->bo_labels)
  851. return -ENOMEM;
  852. vc4->num_labels = VC4_BO_TYPE_COUNT;
  853. BUILD_BUG_ON(ARRAY_SIZE(bo_type_names) != VC4_BO_TYPE_COUNT);
  854. for (i = 0; i < VC4_BO_TYPE_COUNT; i++)
  855. vc4->bo_labels[i].name = bo_type_names[i];
  856. mutex_init(&vc4->bo_lock);
  857. INIT_LIST_HEAD(&vc4->bo_cache.time_list);
  858. INIT_WORK(&vc4->bo_cache.time_work, vc4_bo_cache_time_work);
  859. timer_setup(&vc4->bo_cache.time_timer, vc4_bo_cache_time_timer, 0);
  860. return 0;
  861. }
  862. void vc4_bo_cache_destroy(struct drm_device *dev)
  863. {
  864. struct vc4_dev *vc4 = to_vc4_dev(dev);
  865. int i;
  866. del_timer(&vc4->bo_cache.time_timer);
  867. cancel_work_sync(&vc4->bo_cache.time_work);
  868. vc4_bo_cache_purge(dev);
  869. for (i = 0; i < vc4->num_labels; i++) {
  870. if (vc4->bo_labels[i].num_allocated) {
  871. DRM_ERROR("Destroying BO cache with %d %s "
  872. "BOs still allocated\n",
  873. vc4->bo_labels[i].num_allocated,
  874. vc4->bo_labels[i].name);
  875. }
  876. if (is_user_label(i))
  877. kfree(vc4->bo_labels[i].name);
  878. }
  879. kfree(vc4->bo_labels);
  880. }
  881. int vc4_label_bo_ioctl(struct drm_device *dev, void *data,
  882. struct drm_file *file_priv)
  883. {
  884. struct vc4_dev *vc4 = to_vc4_dev(dev);
  885. struct drm_vc4_label_bo *args = data;
  886. char *name;
  887. struct drm_gem_object *gem_obj;
  888. int ret = 0, label;
  889. if (!args->len)
  890. return -EINVAL;
  891. name = strndup_user(u64_to_user_ptr(args->name), args->len + 1);
  892. if (IS_ERR(name))
  893. return PTR_ERR(name);
  894. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  895. if (!gem_obj) {
  896. DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
  897. kfree(name);
  898. return -ENOENT;
  899. }
  900. mutex_lock(&vc4->bo_lock);
  901. label = vc4_get_user_label(vc4, name);
  902. if (label != -1)
  903. vc4_bo_set_label(gem_obj, label);
  904. else
  905. ret = -ENOMEM;
  906. mutex_unlock(&vc4->bo_lock);
  907. drm_gem_object_put_unlocked(gem_obj);
  908. return ret;
  909. }