vc4_bo.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. refcount_inc(&bo->usecnt);
  531. ret = 0;
  532. break;
  533. case VC4_MADV_DONTNEED:
  534. /* We shouldn't use a BO marked as purgeable if at least
  535. * someone else retained its content by incrementing usecnt.
  536. * Luckily the BO hasn't been purged yet, but something wrong
  537. * is happening here. Just throw an error instead of
  538. * authorizing this use case.
  539. */
  540. case __VC4_MADV_PURGED:
  541. /* We can't use a purged BO. */
  542. default:
  543. /* Invalid madv value. */
  544. ret = -EINVAL;
  545. break;
  546. }
  547. mutex_unlock(&bo->madv_lock);
  548. return ret;
  549. }
  550. void vc4_bo_dec_usecnt(struct vc4_bo *bo)
  551. {
  552. /* Fast path: if the BO is still retained by someone, no need to test
  553. * the madv value.
  554. */
  555. if (refcount_dec_not_one(&bo->usecnt))
  556. return;
  557. mutex_lock(&bo->madv_lock);
  558. if (refcount_dec_and_test(&bo->usecnt) &&
  559. bo->madv == VC4_MADV_DONTNEED)
  560. vc4_bo_add_to_purgeable_pool(bo);
  561. mutex_unlock(&bo->madv_lock);
  562. }
  563. static void vc4_bo_cache_time_timer(struct timer_list *t)
  564. {
  565. struct vc4_dev *vc4 = from_timer(vc4, t, bo_cache.time_timer);
  566. schedule_work(&vc4->bo_cache.time_work);
  567. }
  568. struct reservation_object *vc4_prime_res_obj(struct drm_gem_object *obj)
  569. {
  570. struct vc4_bo *bo = to_vc4_bo(obj);
  571. return bo->resv;
  572. }
  573. struct dma_buf *
  574. vc4_prime_export(struct drm_device *dev, struct drm_gem_object *obj, int flags)
  575. {
  576. struct vc4_bo *bo = to_vc4_bo(obj);
  577. struct dma_buf *dmabuf;
  578. int ret;
  579. if (bo->validated_shader) {
  580. DRM_DEBUG("Attempting to export shader BO\n");
  581. return ERR_PTR(-EINVAL);
  582. }
  583. /* Note: as soon as the BO is exported it becomes unpurgeable, because
  584. * noone ever decrements the usecnt even if the reference held by the
  585. * exported BO is released. This shouldn't be a problem since we don't
  586. * expect exported BOs to be marked as purgeable.
  587. */
  588. ret = vc4_bo_inc_usecnt(bo);
  589. if (ret) {
  590. DRM_ERROR("Failed to increment BO usecnt\n");
  591. return ERR_PTR(ret);
  592. }
  593. dmabuf = drm_gem_prime_export(dev, obj, flags);
  594. if (IS_ERR(dmabuf))
  595. vc4_bo_dec_usecnt(bo);
  596. return dmabuf;
  597. }
  598. int vc4_fault(struct vm_fault *vmf)
  599. {
  600. struct vm_area_struct *vma = vmf->vma;
  601. struct drm_gem_object *obj = vma->vm_private_data;
  602. struct vc4_bo *bo = to_vc4_bo(obj);
  603. /* The only reason we would end up here is when user-space accesses
  604. * BO's memory after it's been purged.
  605. */
  606. mutex_lock(&bo->madv_lock);
  607. WARN_ON(bo->madv != __VC4_MADV_PURGED);
  608. mutex_unlock(&bo->madv_lock);
  609. return VM_FAULT_SIGBUS;
  610. }
  611. int vc4_mmap(struct file *filp, struct vm_area_struct *vma)
  612. {
  613. struct drm_gem_object *gem_obj;
  614. unsigned long vm_pgoff;
  615. struct vc4_bo *bo;
  616. int ret;
  617. ret = drm_gem_mmap(filp, vma);
  618. if (ret)
  619. return ret;
  620. gem_obj = vma->vm_private_data;
  621. bo = to_vc4_bo(gem_obj);
  622. if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
  623. DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
  624. return -EINVAL;
  625. }
  626. if (bo->madv != VC4_MADV_WILLNEED) {
  627. DRM_DEBUG("mmaping of %s BO not allowed\n",
  628. bo->madv == VC4_MADV_DONTNEED ?
  629. "purgeable" : "purged");
  630. return -EINVAL;
  631. }
  632. /*
  633. * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
  634. * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
  635. * the whole buffer.
  636. */
  637. vma->vm_flags &= ~VM_PFNMAP;
  638. /* This ->vm_pgoff dance is needed to make all parties happy:
  639. * - dma_mmap_wc() uses ->vm_pgoff as an offset within the allocated
  640. * mem-region, hence the need to set it to zero (the value set by
  641. * the DRM core is a virtual offset encoding the GEM object-id)
  642. * - the mmap() core logic needs ->vm_pgoff to be restored to its
  643. * initial value before returning from this function because it
  644. * encodes the offset of this GEM in the dev->anon_inode pseudo-file
  645. * and this information will be used when we invalidate userspace
  646. * mappings with drm_vma_node_unmap() (called from vc4_gem_purge()).
  647. */
  648. vm_pgoff = vma->vm_pgoff;
  649. vma->vm_pgoff = 0;
  650. ret = dma_mmap_wc(bo->base.base.dev->dev, vma, bo->base.vaddr,
  651. bo->base.paddr, vma->vm_end - vma->vm_start);
  652. vma->vm_pgoff = vm_pgoff;
  653. if (ret)
  654. drm_gem_vm_close(vma);
  655. return ret;
  656. }
  657. int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
  658. {
  659. struct vc4_bo *bo = to_vc4_bo(obj);
  660. if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
  661. DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
  662. return -EINVAL;
  663. }
  664. return drm_gem_cma_prime_mmap(obj, vma);
  665. }
  666. void *vc4_prime_vmap(struct drm_gem_object *obj)
  667. {
  668. struct vc4_bo *bo = to_vc4_bo(obj);
  669. if (bo->validated_shader) {
  670. DRM_DEBUG("mmaping of shader BOs not allowed.\n");
  671. return ERR_PTR(-EINVAL);
  672. }
  673. return drm_gem_cma_prime_vmap(obj);
  674. }
  675. struct drm_gem_object *
  676. vc4_prime_import_sg_table(struct drm_device *dev,
  677. struct dma_buf_attachment *attach,
  678. struct sg_table *sgt)
  679. {
  680. struct drm_gem_object *obj;
  681. struct vc4_bo *bo;
  682. obj = drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
  683. if (IS_ERR(obj))
  684. return obj;
  685. bo = to_vc4_bo(obj);
  686. bo->resv = attach->dmabuf->resv;
  687. return obj;
  688. }
  689. int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
  690. struct drm_file *file_priv)
  691. {
  692. struct drm_vc4_create_bo *args = data;
  693. struct vc4_bo *bo = NULL;
  694. int ret;
  695. /*
  696. * We can't allocate from the BO cache, because the BOs don't
  697. * get zeroed, and that might leak data between users.
  698. */
  699. bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_V3D);
  700. if (IS_ERR(bo))
  701. return PTR_ERR(bo);
  702. bo->madv = VC4_MADV_WILLNEED;
  703. ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
  704. drm_gem_object_put_unlocked(&bo->base.base);
  705. return ret;
  706. }
  707. int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
  708. struct drm_file *file_priv)
  709. {
  710. struct drm_vc4_mmap_bo *args = data;
  711. struct drm_gem_object *gem_obj;
  712. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  713. if (!gem_obj) {
  714. DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
  715. return -EINVAL;
  716. }
  717. /* The mmap offset was set up at BO allocation time. */
  718. args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
  719. drm_gem_object_put_unlocked(gem_obj);
  720. return 0;
  721. }
  722. int
  723. vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
  724. struct drm_file *file_priv)
  725. {
  726. struct drm_vc4_create_shader_bo *args = data;
  727. struct vc4_bo *bo = NULL;
  728. int ret;
  729. if (args->size == 0)
  730. return -EINVAL;
  731. if (args->size % sizeof(u64) != 0)
  732. return -EINVAL;
  733. if (args->flags != 0) {
  734. DRM_INFO("Unknown flags set: 0x%08x\n", args->flags);
  735. return -EINVAL;
  736. }
  737. if (args->pad != 0) {
  738. DRM_INFO("Pad set: 0x%08x\n", args->pad);
  739. return -EINVAL;
  740. }
  741. bo = vc4_bo_create(dev, args->size, true, VC4_BO_TYPE_V3D_SHADER);
  742. if (IS_ERR(bo))
  743. return PTR_ERR(bo);
  744. bo->madv = VC4_MADV_WILLNEED;
  745. if (copy_from_user(bo->base.vaddr,
  746. (void __user *)(uintptr_t)args->data,
  747. args->size)) {
  748. ret = -EFAULT;
  749. goto fail;
  750. }
  751. /* Clear the rest of the memory from allocating from the BO
  752. * cache.
  753. */
  754. memset(bo->base.vaddr + args->size, 0,
  755. bo->base.base.size - args->size);
  756. bo->validated_shader = vc4_validate_shader(&bo->base);
  757. if (!bo->validated_shader) {
  758. ret = -EINVAL;
  759. goto fail;
  760. }
  761. /* We have to create the handle after validation, to avoid
  762. * races for users to do doing things like mmap the shader BO.
  763. */
  764. ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
  765. fail:
  766. drm_gem_object_put_unlocked(&bo->base.base);
  767. return ret;
  768. }
  769. /**
  770. * vc4_set_tiling_ioctl() - Sets the tiling modifier for a BO.
  771. * @dev: DRM device
  772. * @data: ioctl argument
  773. * @file_priv: DRM file for this fd
  774. *
  775. * The tiling state of the BO decides the default modifier of an fb if
  776. * no specific modifier was set by userspace, and the return value of
  777. * vc4_get_tiling_ioctl() (so that userspace can treat a BO it
  778. * received from dmabuf as the same tiling format as the producer
  779. * used).
  780. */
  781. int vc4_set_tiling_ioctl(struct drm_device *dev, void *data,
  782. struct drm_file *file_priv)
  783. {
  784. struct drm_vc4_set_tiling *args = data;
  785. struct drm_gem_object *gem_obj;
  786. struct vc4_bo *bo;
  787. bool t_format;
  788. if (args->flags != 0)
  789. return -EINVAL;
  790. switch (args->modifier) {
  791. case DRM_FORMAT_MOD_NONE:
  792. t_format = false;
  793. break;
  794. case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
  795. t_format = true;
  796. break;
  797. default:
  798. return -EINVAL;
  799. }
  800. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  801. if (!gem_obj) {
  802. DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
  803. return -ENOENT;
  804. }
  805. bo = to_vc4_bo(gem_obj);
  806. bo->t_format = t_format;
  807. drm_gem_object_put_unlocked(gem_obj);
  808. return 0;
  809. }
  810. /**
  811. * vc4_get_tiling_ioctl() - Gets the tiling modifier for a BO.
  812. * @dev: DRM device
  813. * @data: ioctl argument
  814. * @file_priv: DRM file for this fd
  815. *
  816. * Returns the tiling modifier for a BO as set by vc4_set_tiling_ioctl().
  817. */
  818. int vc4_get_tiling_ioctl(struct drm_device *dev, void *data,
  819. struct drm_file *file_priv)
  820. {
  821. struct drm_vc4_get_tiling *args = data;
  822. struct drm_gem_object *gem_obj;
  823. struct vc4_bo *bo;
  824. if (args->flags != 0 || args->modifier != 0)
  825. return -EINVAL;
  826. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  827. if (!gem_obj) {
  828. DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
  829. return -ENOENT;
  830. }
  831. bo = to_vc4_bo(gem_obj);
  832. if (bo->t_format)
  833. args->modifier = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
  834. else
  835. args->modifier = DRM_FORMAT_MOD_NONE;
  836. drm_gem_object_put_unlocked(gem_obj);
  837. return 0;
  838. }
  839. int vc4_bo_cache_init(struct drm_device *dev)
  840. {
  841. struct vc4_dev *vc4 = to_vc4_dev(dev);
  842. int i;
  843. /* Create the initial set of BO labels that the kernel will
  844. * use. This lets us avoid a bunch of string reallocation in
  845. * the kernel's draw and BO allocation paths.
  846. */
  847. vc4->bo_labels = kcalloc(VC4_BO_TYPE_COUNT, sizeof(*vc4->bo_labels),
  848. GFP_KERNEL);
  849. if (!vc4->bo_labels)
  850. return -ENOMEM;
  851. vc4->num_labels = VC4_BO_TYPE_COUNT;
  852. BUILD_BUG_ON(ARRAY_SIZE(bo_type_names) != VC4_BO_TYPE_COUNT);
  853. for (i = 0; i < VC4_BO_TYPE_COUNT; i++)
  854. vc4->bo_labels[i].name = bo_type_names[i];
  855. mutex_init(&vc4->bo_lock);
  856. INIT_LIST_HEAD(&vc4->bo_cache.time_list);
  857. INIT_WORK(&vc4->bo_cache.time_work, vc4_bo_cache_time_work);
  858. timer_setup(&vc4->bo_cache.time_timer, vc4_bo_cache_time_timer, 0);
  859. return 0;
  860. }
  861. void vc4_bo_cache_destroy(struct drm_device *dev)
  862. {
  863. struct vc4_dev *vc4 = to_vc4_dev(dev);
  864. int i;
  865. del_timer(&vc4->bo_cache.time_timer);
  866. cancel_work_sync(&vc4->bo_cache.time_work);
  867. vc4_bo_cache_purge(dev);
  868. for (i = 0; i < vc4->num_labels; i++) {
  869. if (vc4->bo_labels[i].num_allocated) {
  870. DRM_ERROR("Destroying BO cache with %d %s "
  871. "BOs still allocated\n",
  872. vc4->bo_labels[i].num_allocated,
  873. vc4->bo_labels[i].name);
  874. }
  875. if (is_user_label(i))
  876. kfree(vc4->bo_labels[i].name);
  877. }
  878. kfree(vc4->bo_labels);
  879. }
  880. int vc4_label_bo_ioctl(struct drm_device *dev, void *data,
  881. struct drm_file *file_priv)
  882. {
  883. struct vc4_dev *vc4 = to_vc4_dev(dev);
  884. struct drm_vc4_label_bo *args = data;
  885. char *name;
  886. struct drm_gem_object *gem_obj;
  887. int ret = 0, label;
  888. if (!args->len)
  889. return -EINVAL;
  890. name = strndup_user(u64_to_user_ptr(args->name), args->len + 1);
  891. if (IS_ERR(name))
  892. return PTR_ERR(name);
  893. gem_obj = drm_gem_object_lookup(file_priv, args->handle);
  894. if (!gem_obj) {
  895. DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
  896. kfree(name);
  897. return -ENOENT;
  898. }
  899. mutex_lock(&vc4->bo_lock);
  900. label = vc4_get_user_label(vc4, name);
  901. if (label != -1)
  902. vc4_bo_set_label(gem_obj, label);
  903. else
  904. ret = -ENOMEM;
  905. mutex_unlock(&vc4->bo_lock);
  906. drm_gem_object_put_unlocked(gem_obj);
  907. return ret;
  908. }