vc4_bo.c 28 KB

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