vc4_bo.c 27 KB

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