etnaviv_drv.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015-2018 Etnaviv Project
  4. */
  5. #include <linux/component.h>
  6. #include <linux/dma-mapping.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/of_device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/uaccess.h>
  12. #include <drm/drm_debugfs.h>
  13. #include <drm/drm_drv.h>
  14. #include <drm/drm_file.h>
  15. #include <drm/drm_ioctl.h>
  16. #include <drm/drm_of.h>
  17. #include <drm/drm_prime.h>
  18. #include "etnaviv_cmdbuf.h"
  19. #include "etnaviv_drv.h"
  20. #include "etnaviv_gpu.h"
  21. #include "etnaviv_gem.h"
  22. #include "etnaviv_mmu.h"
  23. #include "etnaviv_perfmon.h"
  24. /*
  25. * DRM operations:
  26. */
  27. static struct device_node *etnaviv_of_first_available_node(void)
  28. {
  29. struct device_node *np;
  30. for_each_compatible_node(np, NULL, "vivante,gc") {
  31. if (of_device_is_available(np))
  32. return np;
  33. }
  34. return NULL;
  35. }
  36. static void load_gpu(struct drm_device *dev)
  37. {
  38. struct etnaviv_drm_private *priv = dev->dev_private;
  39. unsigned int i;
  40. for (i = 0; i < ETNA_MAX_PIPES; i++) {
  41. struct etnaviv_gpu *g = priv->gpu[i];
  42. if (g) {
  43. int ret;
  44. ret = etnaviv_gpu_init(g);
  45. if (ret)
  46. priv->gpu[i] = NULL;
  47. }
  48. }
  49. }
  50. static int etnaviv_open(struct drm_device *dev, struct drm_file *file)
  51. {
  52. struct etnaviv_drm_private *priv = dev->dev_private;
  53. struct etnaviv_file_private *ctx;
  54. int ret, i;
  55. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  56. if (!ctx)
  57. return -ENOMEM;
  58. ret = xa_alloc_cyclic(&priv->active_contexts, &ctx->id, ctx,
  59. xa_limit_32b, &priv->next_context_id, GFP_KERNEL);
  60. if (ret < 0)
  61. goto out_free;
  62. ctx->mmu = etnaviv_iommu_context_init(priv->mmu_global,
  63. priv->cmdbuf_suballoc);
  64. if (!ctx->mmu) {
  65. ret = -ENOMEM;
  66. goto out_free;
  67. }
  68. for (i = 0; i < ETNA_MAX_PIPES; i++) {
  69. struct etnaviv_gpu *gpu = priv->gpu[i];
  70. struct drm_gpu_scheduler *sched;
  71. if (gpu) {
  72. sched = &gpu->sched;
  73. drm_sched_entity_init(&ctx->sched_entity[i],
  74. DRM_SCHED_PRIORITY_NORMAL, &sched,
  75. 1, NULL);
  76. }
  77. }
  78. file->driver_priv = ctx;
  79. return 0;
  80. out_free:
  81. kfree(ctx);
  82. return ret;
  83. }
  84. static void etnaviv_postclose(struct drm_device *dev, struct drm_file *file)
  85. {
  86. struct etnaviv_drm_private *priv = dev->dev_private;
  87. struct etnaviv_file_private *ctx = file->driver_priv;
  88. unsigned int i;
  89. for (i = 0; i < ETNA_MAX_PIPES; i++) {
  90. struct etnaviv_gpu *gpu = priv->gpu[i];
  91. if (gpu)
  92. drm_sched_entity_destroy(&ctx->sched_entity[i]);
  93. }
  94. etnaviv_iommu_context_put(ctx->mmu);
  95. xa_erase(&priv->active_contexts, ctx->id);
  96. kfree(ctx);
  97. }
  98. /*
  99. * DRM debugfs:
  100. */
  101. #ifdef CONFIG_DEBUG_FS
  102. static int etnaviv_gem_show(struct drm_device *dev, struct seq_file *m)
  103. {
  104. struct etnaviv_drm_private *priv = dev->dev_private;
  105. etnaviv_gem_describe_objects(priv, m);
  106. return 0;
  107. }
  108. static int etnaviv_mm_show(struct drm_device *dev, struct seq_file *m)
  109. {
  110. struct drm_printer p = drm_seq_file_printer(m);
  111. read_lock(&dev->vma_offset_manager->vm_lock);
  112. drm_mm_print(&dev->vma_offset_manager->vm_addr_space_mm, &p);
  113. read_unlock(&dev->vma_offset_manager->vm_lock);
  114. return 0;
  115. }
  116. static int etnaviv_mmu_show(struct etnaviv_gpu *gpu, struct seq_file *m)
  117. {
  118. struct drm_printer p = drm_seq_file_printer(m);
  119. struct etnaviv_iommu_context *mmu_context;
  120. seq_printf(m, "Active Objects (%s):\n", dev_name(gpu->dev));
  121. /*
  122. * Lock the GPU to avoid a MMU context switch just now and elevate
  123. * the refcount of the current context to avoid it disappearing from
  124. * under our feet.
  125. */
  126. mutex_lock(&gpu->lock);
  127. mmu_context = gpu->mmu_context;
  128. if (mmu_context)
  129. etnaviv_iommu_context_get(mmu_context);
  130. mutex_unlock(&gpu->lock);
  131. if (!mmu_context)
  132. return 0;
  133. mutex_lock(&mmu_context->lock);
  134. drm_mm_print(&mmu_context->mm, &p);
  135. mutex_unlock(&mmu_context->lock);
  136. etnaviv_iommu_context_put(mmu_context);
  137. return 0;
  138. }
  139. static void etnaviv_buffer_dump(struct etnaviv_gpu *gpu, struct seq_file *m)
  140. {
  141. struct etnaviv_cmdbuf *buf = &gpu->buffer;
  142. u32 size = buf->size;
  143. u32 *ptr = buf->vaddr;
  144. u32 i;
  145. seq_printf(m, "virt %p - phys 0x%llx - free 0x%08x\n",
  146. buf->vaddr, (u64)etnaviv_cmdbuf_get_pa(buf),
  147. size - buf->user_size);
  148. for (i = 0; i < size / 4; i++) {
  149. if (i && !(i % 4))
  150. seq_puts(m, "\n");
  151. if (i % 4 == 0)
  152. seq_printf(m, "\t0x%p: ", ptr + i);
  153. seq_printf(m, "%08x ", *(ptr + i));
  154. }
  155. seq_puts(m, "\n");
  156. }
  157. static int etnaviv_ring_show(struct etnaviv_gpu *gpu, struct seq_file *m)
  158. {
  159. seq_printf(m, "Ring Buffer (%s): ", dev_name(gpu->dev));
  160. mutex_lock(&gpu->lock);
  161. etnaviv_buffer_dump(gpu, m);
  162. mutex_unlock(&gpu->lock);
  163. return 0;
  164. }
  165. static int show_unlocked(struct seq_file *m, void *arg)
  166. {
  167. struct drm_info_node *node = (struct drm_info_node *) m->private;
  168. struct drm_device *dev = node->minor->dev;
  169. int (*show)(struct drm_device *dev, struct seq_file *m) =
  170. node->info_ent->data;
  171. return show(dev, m);
  172. }
  173. static int show_each_gpu(struct seq_file *m, void *arg)
  174. {
  175. struct drm_info_node *node = (struct drm_info_node *) m->private;
  176. struct drm_device *dev = node->minor->dev;
  177. struct etnaviv_drm_private *priv = dev->dev_private;
  178. struct etnaviv_gpu *gpu;
  179. int (*show)(struct etnaviv_gpu *gpu, struct seq_file *m) =
  180. node->info_ent->data;
  181. unsigned int i;
  182. int ret = 0;
  183. for (i = 0; i < ETNA_MAX_PIPES; i++) {
  184. gpu = priv->gpu[i];
  185. if (!gpu)
  186. continue;
  187. ret = show(gpu, m);
  188. if (ret < 0)
  189. break;
  190. }
  191. return ret;
  192. }
  193. static struct drm_info_list etnaviv_debugfs_list[] = {
  194. {"gpu", show_each_gpu, 0, etnaviv_gpu_debugfs},
  195. {"gem", show_unlocked, 0, etnaviv_gem_show},
  196. { "mm", show_unlocked, 0, etnaviv_mm_show },
  197. {"mmu", show_each_gpu, 0, etnaviv_mmu_show},
  198. {"ring", show_each_gpu, 0, etnaviv_ring_show},
  199. };
  200. static void etnaviv_debugfs_init(struct drm_minor *minor)
  201. {
  202. drm_debugfs_create_files(etnaviv_debugfs_list,
  203. ARRAY_SIZE(etnaviv_debugfs_list),
  204. minor->debugfs_root, minor);
  205. }
  206. #endif
  207. /*
  208. * DRM ioctls:
  209. */
  210. static int etnaviv_ioctl_get_param(struct drm_device *dev, void *data,
  211. struct drm_file *file)
  212. {
  213. struct etnaviv_drm_private *priv = dev->dev_private;
  214. struct drm_etnaviv_param *args = data;
  215. struct etnaviv_gpu *gpu;
  216. if (args->pipe >= ETNA_MAX_PIPES)
  217. return -EINVAL;
  218. gpu = priv->gpu[args->pipe];
  219. if (!gpu)
  220. return -ENXIO;
  221. return etnaviv_gpu_get_param(gpu, args->param, &args->value);
  222. }
  223. static int etnaviv_ioctl_gem_new(struct drm_device *dev, void *data,
  224. struct drm_file *file)
  225. {
  226. struct drm_etnaviv_gem_new *args = data;
  227. if (args->flags & ~(ETNA_BO_CACHED | ETNA_BO_WC | ETNA_BO_UNCACHED |
  228. ETNA_BO_FORCE_MMU))
  229. return -EINVAL;
  230. return etnaviv_gem_new_handle(dev, file, args->size,
  231. args->flags, &args->handle);
  232. }
  233. static int etnaviv_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
  234. struct drm_file *file)
  235. {
  236. struct drm_etnaviv_gem_cpu_prep *args = data;
  237. struct drm_gem_object *obj;
  238. int ret;
  239. if (args->op & ~(ETNA_PREP_READ | ETNA_PREP_WRITE | ETNA_PREP_NOSYNC))
  240. return -EINVAL;
  241. obj = drm_gem_object_lookup(file, args->handle);
  242. if (!obj)
  243. return -ENOENT;
  244. ret = etnaviv_gem_cpu_prep(obj, args->op, &args->timeout);
  245. drm_gem_object_put(obj);
  246. return ret;
  247. }
  248. static int etnaviv_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
  249. struct drm_file *file)
  250. {
  251. struct drm_etnaviv_gem_cpu_fini *args = data;
  252. struct drm_gem_object *obj;
  253. int ret;
  254. if (args->flags)
  255. return -EINVAL;
  256. obj = drm_gem_object_lookup(file, args->handle);
  257. if (!obj)
  258. return -ENOENT;
  259. ret = etnaviv_gem_cpu_fini(obj);
  260. drm_gem_object_put(obj);
  261. return ret;
  262. }
  263. static int etnaviv_ioctl_gem_info(struct drm_device *dev, void *data,
  264. struct drm_file *file)
  265. {
  266. struct drm_etnaviv_gem_info *args = data;
  267. struct drm_gem_object *obj;
  268. int ret;
  269. if (args->pad)
  270. return -EINVAL;
  271. obj = drm_gem_object_lookup(file, args->handle);
  272. if (!obj)
  273. return -ENOENT;
  274. ret = etnaviv_gem_mmap_offset(obj, &args->offset);
  275. drm_gem_object_put(obj);
  276. return ret;
  277. }
  278. static int etnaviv_ioctl_wait_fence(struct drm_device *dev, void *data,
  279. struct drm_file *file)
  280. {
  281. struct drm_etnaviv_wait_fence *args = data;
  282. struct etnaviv_drm_private *priv = dev->dev_private;
  283. struct drm_etnaviv_timespec *timeout = &args->timeout;
  284. struct etnaviv_gpu *gpu;
  285. if (args->flags & ~(ETNA_WAIT_NONBLOCK))
  286. return -EINVAL;
  287. if (args->pipe >= ETNA_MAX_PIPES)
  288. return -EINVAL;
  289. gpu = priv->gpu[args->pipe];
  290. if (!gpu)
  291. return -ENXIO;
  292. if (args->flags & ETNA_WAIT_NONBLOCK)
  293. timeout = NULL;
  294. return etnaviv_gpu_wait_fence_interruptible(gpu, args->fence,
  295. timeout);
  296. }
  297. static int etnaviv_ioctl_gem_userptr(struct drm_device *dev, void *data,
  298. struct drm_file *file)
  299. {
  300. struct drm_etnaviv_gem_userptr *args = data;
  301. if (args->flags & ~(ETNA_USERPTR_READ|ETNA_USERPTR_WRITE) ||
  302. args->flags == 0)
  303. return -EINVAL;
  304. if (offset_in_page(args->user_ptr | args->user_size) ||
  305. (uintptr_t)args->user_ptr != args->user_ptr ||
  306. (u32)args->user_size != args->user_size ||
  307. args->user_ptr & ~PAGE_MASK)
  308. return -EINVAL;
  309. if (!access_ok((void __user *)(unsigned long)args->user_ptr,
  310. args->user_size))
  311. return -EFAULT;
  312. return etnaviv_gem_new_userptr(dev, file, args->user_ptr,
  313. args->user_size, args->flags,
  314. &args->handle);
  315. }
  316. static int etnaviv_ioctl_gem_wait(struct drm_device *dev, void *data,
  317. struct drm_file *file)
  318. {
  319. struct etnaviv_drm_private *priv = dev->dev_private;
  320. struct drm_etnaviv_gem_wait *args = data;
  321. struct drm_etnaviv_timespec *timeout = &args->timeout;
  322. struct drm_gem_object *obj;
  323. struct etnaviv_gpu *gpu;
  324. int ret;
  325. if (args->flags & ~(ETNA_WAIT_NONBLOCK))
  326. return -EINVAL;
  327. if (args->pipe >= ETNA_MAX_PIPES)
  328. return -EINVAL;
  329. gpu = priv->gpu[args->pipe];
  330. if (!gpu)
  331. return -ENXIO;
  332. obj = drm_gem_object_lookup(file, args->handle);
  333. if (!obj)
  334. return -ENOENT;
  335. if (args->flags & ETNA_WAIT_NONBLOCK)
  336. timeout = NULL;
  337. ret = etnaviv_gem_wait_bo(gpu, obj, timeout);
  338. drm_gem_object_put(obj);
  339. return ret;
  340. }
  341. static int etnaviv_ioctl_pm_query_dom(struct drm_device *dev, void *data,
  342. struct drm_file *file)
  343. {
  344. struct etnaviv_drm_private *priv = dev->dev_private;
  345. struct drm_etnaviv_pm_domain *args = data;
  346. struct etnaviv_gpu *gpu;
  347. if (args->pipe >= ETNA_MAX_PIPES)
  348. return -EINVAL;
  349. gpu = priv->gpu[args->pipe];
  350. if (!gpu)
  351. return -ENXIO;
  352. return etnaviv_pm_query_dom(gpu, args);
  353. }
  354. static int etnaviv_ioctl_pm_query_sig(struct drm_device *dev, void *data,
  355. struct drm_file *file)
  356. {
  357. struct etnaviv_drm_private *priv = dev->dev_private;
  358. struct drm_etnaviv_pm_signal *args = data;
  359. struct etnaviv_gpu *gpu;
  360. if (args->pipe >= ETNA_MAX_PIPES)
  361. return -EINVAL;
  362. gpu = priv->gpu[args->pipe];
  363. if (!gpu)
  364. return -ENXIO;
  365. return etnaviv_pm_query_sig(gpu, args);
  366. }
  367. static const struct drm_ioctl_desc etnaviv_ioctls[] = {
  368. #define ETNA_IOCTL(n, func, flags) \
  369. DRM_IOCTL_DEF_DRV(ETNAVIV_##n, etnaviv_ioctl_##func, flags)
  370. ETNA_IOCTL(GET_PARAM, get_param, DRM_RENDER_ALLOW),
  371. ETNA_IOCTL(GEM_NEW, gem_new, DRM_RENDER_ALLOW),
  372. ETNA_IOCTL(GEM_INFO, gem_info, DRM_RENDER_ALLOW),
  373. ETNA_IOCTL(GEM_CPU_PREP, gem_cpu_prep, DRM_RENDER_ALLOW),
  374. ETNA_IOCTL(GEM_CPU_FINI, gem_cpu_fini, DRM_RENDER_ALLOW),
  375. ETNA_IOCTL(GEM_SUBMIT, gem_submit, DRM_RENDER_ALLOW),
  376. ETNA_IOCTL(WAIT_FENCE, wait_fence, DRM_RENDER_ALLOW),
  377. ETNA_IOCTL(GEM_USERPTR, gem_userptr, DRM_RENDER_ALLOW),
  378. ETNA_IOCTL(GEM_WAIT, gem_wait, DRM_RENDER_ALLOW),
  379. ETNA_IOCTL(PM_QUERY_DOM, pm_query_dom, DRM_RENDER_ALLOW),
  380. ETNA_IOCTL(PM_QUERY_SIG, pm_query_sig, DRM_RENDER_ALLOW),
  381. };
  382. DEFINE_DRM_GEM_FOPS(fops);
  383. static const struct drm_driver etnaviv_drm_driver = {
  384. .driver_features = DRIVER_GEM | DRIVER_RENDER,
  385. .open = etnaviv_open,
  386. .postclose = etnaviv_postclose,
  387. .gem_prime_import_sg_table = etnaviv_gem_prime_import_sg_table,
  388. #ifdef CONFIG_DEBUG_FS
  389. .debugfs_init = etnaviv_debugfs_init,
  390. #endif
  391. .ioctls = etnaviv_ioctls,
  392. .num_ioctls = DRM_ETNAVIV_NUM_IOCTLS,
  393. .fops = &fops,
  394. .name = "etnaviv",
  395. .desc = "etnaviv DRM",
  396. .date = "20151214",
  397. .major = 1,
  398. .minor = 4,
  399. };
  400. /*
  401. * Platform driver:
  402. */
  403. static int etnaviv_bind(struct device *dev)
  404. {
  405. struct etnaviv_drm_private *priv;
  406. struct drm_device *drm;
  407. int ret;
  408. drm = drm_dev_alloc(&etnaviv_drm_driver, dev);
  409. if (IS_ERR(drm))
  410. return PTR_ERR(drm);
  411. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  412. if (!priv) {
  413. dev_err(dev, "failed to allocate private data\n");
  414. ret = -ENOMEM;
  415. goto out_put;
  416. }
  417. drm->dev_private = priv;
  418. dma_set_max_seg_size(dev, SZ_2G);
  419. xa_init_flags(&priv->active_contexts, XA_FLAGS_ALLOC);
  420. mutex_init(&priv->gem_lock);
  421. INIT_LIST_HEAD(&priv->gem_list);
  422. priv->num_gpus = 0;
  423. priv->shm_gfp_mask = GFP_HIGHUSER | __GFP_RETRY_MAYFAIL | __GFP_NOWARN;
  424. /*
  425. * If the GPU is part of a system with DMA addressing limitations,
  426. * request pages for our SHM backend buffers from the DMA32 zone to
  427. * hopefully avoid performance killing SWIOTLB bounce buffering.
  428. */
  429. if (dma_addressing_limited(dev)) {
  430. priv->shm_gfp_mask |= GFP_DMA32;
  431. priv->shm_gfp_mask &= ~__GFP_HIGHMEM;
  432. }
  433. priv->cmdbuf_suballoc = etnaviv_cmdbuf_suballoc_new(drm->dev);
  434. if (IS_ERR(priv->cmdbuf_suballoc)) {
  435. dev_err(drm->dev, "Failed to create cmdbuf suballocator\n");
  436. ret = PTR_ERR(priv->cmdbuf_suballoc);
  437. goto out_free_priv;
  438. }
  439. dev_set_drvdata(dev, drm);
  440. ret = component_bind_all(dev, drm);
  441. if (ret < 0)
  442. goto out_destroy_suballoc;
  443. load_gpu(drm);
  444. ret = drm_dev_register(drm, 0);
  445. if (ret)
  446. goto out_unbind;
  447. return 0;
  448. out_unbind:
  449. component_unbind_all(dev, drm);
  450. out_destroy_suballoc:
  451. etnaviv_cmdbuf_suballoc_destroy(priv->cmdbuf_suballoc);
  452. out_free_priv:
  453. kfree(priv);
  454. out_put:
  455. drm_dev_put(drm);
  456. return ret;
  457. }
  458. static void etnaviv_unbind(struct device *dev)
  459. {
  460. struct drm_device *drm = dev_get_drvdata(dev);
  461. struct etnaviv_drm_private *priv = drm->dev_private;
  462. drm_dev_unregister(drm);
  463. component_unbind_all(dev, drm);
  464. etnaviv_cmdbuf_suballoc_destroy(priv->cmdbuf_suballoc);
  465. xa_destroy(&priv->active_contexts);
  466. drm->dev_private = NULL;
  467. kfree(priv);
  468. drm_dev_put(drm);
  469. }
  470. static const struct component_master_ops etnaviv_master_ops = {
  471. .bind = etnaviv_bind,
  472. .unbind = etnaviv_unbind,
  473. };
  474. static int etnaviv_pdev_probe(struct platform_device *pdev)
  475. {
  476. struct device *dev = &pdev->dev;
  477. struct device_node *first_node = NULL;
  478. struct component_match *match = NULL;
  479. if (!dev->platform_data) {
  480. struct device_node *core_node;
  481. for_each_compatible_node(core_node, NULL, "vivante,gc") {
  482. if (!of_device_is_available(core_node))
  483. continue;
  484. drm_of_component_match_add(&pdev->dev, &match,
  485. component_compare_of, core_node);
  486. }
  487. } else {
  488. char **names = dev->platform_data;
  489. unsigned i;
  490. for (i = 0; names[i]; i++)
  491. component_match_add(dev, &match, component_compare_dev_name, names[i]);
  492. }
  493. /*
  494. * PTA and MTLB can have 40 bit base addresses, but
  495. * unfortunately, an entry in the MTLB can only point to a
  496. * 32 bit base address of a STLB. Moreover, to initialize the
  497. * MMU we need a command buffer with a 32 bit address because
  498. * without an MMU there is only an indentity mapping between
  499. * the internal 32 bit addresses and the bus addresses.
  500. *
  501. * To make things easy, we set the dma_coherent_mask to 32
  502. * bit to make sure we are allocating the command buffers and
  503. * TLBs in the lower 4 GiB address space.
  504. */
  505. if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(40)) ||
  506. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32))) {
  507. dev_dbg(&pdev->dev, "No suitable DMA available\n");
  508. return -ENODEV;
  509. }
  510. /*
  511. * Apply the same DMA configuration to the virtual etnaviv
  512. * device as the GPU we found. This assumes that all Vivante
  513. * GPUs in the system share the same DMA constraints.
  514. */
  515. first_node = etnaviv_of_first_available_node();
  516. if (first_node) {
  517. of_dma_configure(&pdev->dev, first_node, true);
  518. of_node_put(first_node);
  519. }
  520. return component_master_add_with_match(dev, &etnaviv_master_ops, match);
  521. }
  522. static void etnaviv_pdev_remove(struct platform_device *pdev)
  523. {
  524. component_master_del(&pdev->dev, &etnaviv_master_ops);
  525. }
  526. static struct platform_driver etnaviv_platform_driver = {
  527. .probe = etnaviv_pdev_probe,
  528. .remove_new = etnaviv_pdev_remove,
  529. .driver = {
  530. .name = "etnaviv",
  531. },
  532. };
  533. static int etnaviv_create_platform_device(const char *name,
  534. struct platform_device **ppdev)
  535. {
  536. struct platform_device *pdev;
  537. int ret;
  538. pdev = platform_device_alloc(name, PLATFORM_DEVID_NONE);
  539. if (!pdev)
  540. return -ENOMEM;
  541. ret = platform_device_add(pdev);
  542. if (ret) {
  543. platform_device_put(pdev);
  544. return ret;
  545. }
  546. *ppdev = pdev;
  547. return 0;
  548. }
  549. static void etnaviv_destroy_platform_device(struct platform_device **ppdev)
  550. {
  551. struct platform_device *pdev = *ppdev;
  552. if (!pdev)
  553. return;
  554. platform_device_unregister(pdev);
  555. *ppdev = NULL;
  556. }
  557. static struct platform_device *etnaviv_drm;
  558. static int __init etnaviv_init(void)
  559. {
  560. int ret;
  561. struct device_node *np;
  562. etnaviv_validate_init();
  563. ret = platform_driver_register(&etnaviv_gpu_driver);
  564. if (ret != 0)
  565. return ret;
  566. ret = platform_driver_register(&etnaviv_platform_driver);
  567. if (ret != 0)
  568. goto unregister_gpu_driver;
  569. /*
  570. * If the DT contains at least one available GPU device, instantiate
  571. * the DRM platform device.
  572. */
  573. np = etnaviv_of_first_available_node();
  574. if (np) {
  575. of_node_put(np);
  576. ret = etnaviv_create_platform_device("etnaviv", &etnaviv_drm);
  577. if (ret)
  578. goto unregister_platform_driver;
  579. }
  580. return 0;
  581. unregister_platform_driver:
  582. platform_driver_unregister(&etnaviv_platform_driver);
  583. unregister_gpu_driver:
  584. platform_driver_unregister(&etnaviv_gpu_driver);
  585. return ret;
  586. }
  587. module_init(etnaviv_init);
  588. static void __exit etnaviv_exit(void)
  589. {
  590. etnaviv_destroy_platform_device(&etnaviv_drm);
  591. platform_driver_unregister(&etnaviv_platform_driver);
  592. platform_driver_unregister(&etnaviv_gpu_driver);
  593. }
  594. module_exit(etnaviv_exit);
  595. MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
  596. MODULE_AUTHOR("Russell King <rmk+kernel@armlinux.org.uk>");
  597. MODULE_AUTHOR("Lucas Stach <l.stach@pengutronix.de>");
  598. MODULE_DESCRIPTION("etnaviv DRM Driver");
  599. MODULE_LICENSE("GPL v2");
  600. MODULE_ALIAS("platform:etnaviv");