msm_drv.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "msm_drv.h"
  18. #include "msm_gpu.h"
  19. #include "msm_kms.h"
  20. static void msm_fb_output_poll_changed(struct drm_device *dev)
  21. {
  22. #ifdef CONFIG_DRM_MSM_FBDEV
  23. struct msm_drm_private *priv = dev->dev_private;
  24. if (priv->fbdev)
  25. drm_fb_helper_hotplug_event(priv->fbdev);
  26. #endif
  27. }
  28. static const struct drm_mode_config_funcs mode_config_funcs = {
  29. .fb_create = msm_framebuffer_create,
  30. .output_poll_changed = msm_fb_output_poll_changed,
  31. .atomic_check = msm_atomic_check,
  32. .atomic_commit = msm_atomic_commit,
  33. };
  34. int msm_register_mmu(struct drm_device *dev, struct msm_mmu *mmu)
  35. {
  36. struct msm_drm_private *priv = dev->dev_private;
  37. int idx = priv->num_mmus++;
  38. if (WARN_ON(idx >= ARRAY_SIZE(priv->mmus)))
  39. return -EINVAL;
  40. priv->mmus[idx] = mmu;
  41. return idx;
  42. }
  43. #ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
  44. static bool reglog = false;
  45. MODULE_PARM_DESC(reglog, "Enable register read/write logging");
  46. module_param(reglog, bool, 0600);
  47. #else
  48. #define reglog 0
  49. #endif
  50. #ifdef CONFIG_DRM_MSM_FBDEV
  51. static bool fbdev = true;
  52. MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
  53. module_param(fbdev, bool, 0600);
  54. #endif
  55. static char *vram = "16m";
  56. MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU");
  57. module_param(vram, charp, 0);
  58. /*
  59. * Util/helpers:
  60. */
  61. void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
  62. const char *dbgname)
  63. {
  64. struct resource *res;
  65. unsigned long size;
  66. void __iomem *ptr;
  67. if (name)
  68. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  69. else
  70. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  71. if (!res) {
  72. dev_err(&pdev->dev, "failed to get memory resource: %s\n", name);
  73. return ERR_PTR(-EINVAL);
  74. }
  75. size = resource_size(res);
  76. ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
  77. if (!ptr) {
  78. dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
  79. return ERR_PTR(-ENOMEM);
  80. }
  81. if (reglog)
  82. printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
  83. return ptr;
  84. }
  85. void msm_writel(u32 data, void __iomem *addr)
  86. {
  87. if (reglog)
  88. printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
  89. writel(data, addr);
  90. }
  91. u32 msm_readl(const void __iomem *addr)
  92. {
  93. u32 val = readl(addr);
  94. if (reglog)
  95. printk(KERN_ERR "IO:R %p %08x\n", addr, val);
  96. return val;
  97. }
  98. /*
  99. * DRM operations:
  100. */
  101. static int msm_unload(struct drm_device *dev)
  102. {
  103. struct msm_drm_private *priv = dev->dev_private;
  104. struct msm_kms *kms = priv->kms;
  105. struct msm_gpu *gpu = priv->gpu;
  106. drm_kms_helper_poll_fini(dev);
  107. drm_mode_config_cleanup(dev);
  108. drm_vblank_cleanup(dev);
  109. pm_runtime_get_sync(dev->dev);
  110. drm_irq_uninstall(dev);
  111. pm_runtime_put_sync(dev->dev);
  112. flush_workqueue(priv->wq);
  113. destroy_workqueue(priv->wq);
  114. if (kms) {
  115. pm_runtime_disable(dev->dev);
  116. kms->funcs->destroy(kms);
  117. }
  118. if (gpu) {
  119. mutex_lock(&dev->struct_mutex);
  120. gpu->funcs->pm_suspend(gpu);
  121. mutex_unlock(&dev->struct_mutex);
  122. gpu->funcs->destroy(gpu);
  123. }
  124. if (priv->vram.paddr) {
  125. DEFINE_DMA_ATTRS(attrs);
  126. dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
  127. drm_mm_takedown(&priv->vram.mm);
  128. dma_free_attrs(dev->dev, priv->vram.size, NULL,
  129. priv->vram.paddr, &attrs);
  130. }
  131. component_unbind_all(dev->dev, dev);
  132. dev->dev_private = NULL;
  133. kfree(priv);
  134. return 0;
  135. }
  136. static int get_mdp_ver(struct platform_device *pdev)
  137. {
  138. #ifdef CONFIG_OF
  139. static const struct of_device_id match_types[] = { {
  140. .compatible = "qcom,mdss_mdp",
  141. .data = (void *)5,
  142. }, {
  143. /* end node */
  144. } };
  145. struct device *dev = &pdev->dev;
  146. const struct of_device_id *match;
  147. match = of_match_node(match_types, dev->of_node);
  148. if (match)
  149. return (int)(unsigned long)match->data;
  150. #endif
  151. return 4;
  152. }
  153. #include <linux/of_address.h>
  154. static int msm_init_vram(struct drm_device *dev)
  155. {
  156. struct msm_drm_private *priv = dev->dev_private;
  157. unsigned long size = 0;
  158. int ret = 0;
  159. #ifdef CONFIG_OF
  160. /* In the device-tree world, we could have a 'memory-region'
  161. * phandle, which gives us a link to our "vram". Allocating
  162. * is all nicely abstracted behind the dma api, but we need
  163. * to know the entire size to allocate it all in one go. There
  164. * are two cases:
  165. * 1) device with no IOMMU, in which case we need exclusive
  166. * access to a VRAM carveout big enough for all gpu
  167. * buffers
  168. * 2) device with IOMMU, but where the bootloader puts up
  169. * a splash screen. In this case, the VRAM carveout
  170. * need only be large enough for fbdev fb. But we need
  171. * exclusive access to the buffer to avoid the kernel
  172. * using those pages for other purposes (which appears
  173. * as corruption on screen before we have a chance to
  174. * load and do initial modeset)
  175. */
  176. struct device_node *node;
  177. node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
  178. if (node) {
  179. struct resource r;
  180. ret = of_address_to_resource(node, 0, &r);
  181. if (ret)
  182. return ret;
  183. size = r.end - r.start;
  184. DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
  185. } else
  186. #endif
  187. /* if we have no IOMMU, then we need to use carveout allocator.
  188. * Grab the entire CMA chunk carved out in early startup in
  189. * mach-msm:
  190. */
  191. if (!iommu_present(&platform_bus_type)) {
  192. DRM_INFO("using %s VRAM carveout\n", vram);
  193. size = memparse(vram, NULL);
  194. }
  195. if (size) {
  196. DEFINE_DMA_ATTRS(attrs);
  197. void *p;
  198. priv->vram.size = size;
  199. drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
  200. dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
  201. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  202. /* note that for no-kernel-mapping, the vaddr returned
  203. * is bogus, but non-null if allocation succeeded:
  204. */
  205. p = dma_alloc_attrs(dev->dev, size,
  206. &priv->vram.paddr, GFP_KERNEL, &attrs);
  207. if (!p) {
  208. dev_err(dev->dev, "failed to allocate VRAM\n");
  209. priv->vram.paddr = 0;
  210. return -ENOMEM;
  211. }
  212. dev_info(dev->dev, "VRAM: %08x->%08x\n",
  213. (uint32_t)priv->vram.paddr,
  214. (uint32_t)(priv->vram.paddr + size));
  215. }
  216. return ret;
  217. }
  218. static int msm_load(struct drm_device *dev, unsigned long flags)
  219. {
  220. struct platform_device *pdev = dev->platformdev;
  221. struct msm_drm_private *priv;
  222. struct msm_kms *kms;
  223. int ret;
  224. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  225. if (!priv) {
  226. dev_err(dev->dev, "failed to allocate private data\n");
  227. return -ENOMEM;
  228. }
  229. dev->dev_private = priv;
  230. priv->wq = alloc_ordered_workqueue("msm", 0);
  231. init_waitqueue_head(&priv->fence_event);
  232. init_waitqueue_head(&priv->pending_crtcs_event);
  233. INIT_LIST_HEAD(&priv->inactive_list);
  234. INIT_LIST_HEAD(&priv->fence_cbs);
  235. drm_mode_config_init(dev);
  236. platform_set_drvdata(pdev, dev);
  237. /* Bind all our sub-components: */
  238. ret = component_bind_all(dev->dev, dev);
  239. if (ret)
  240. return ret;
  241. ret = msm_init_vram(dev);
  242. if (ret)
  243. goto fail;
  244. switch (get_mdp_ver(pdev)) {
  245. case 4:
  246. kms = mdp4_kms_init(dev);
  247. break;
  248. case 5:
  249. kms = mdp5_kms_init(dev);
  250. break;
  251. default:
  252. kms = ERR_PTR(-ENODEV);
  253. break;
  254. }
  255. if (IS_ERR(kms)) {
  256. /*
  257. * NOTE: once we have GPU support, having no kms should not
  258. * be considered fatal.. ideally we would still support gpu
  259. * and (for example) use dmabuf/prime to share buffers with
  260. * imx drm driver on iMX5
  261. */
  262. dev_err(dev->dev, "failed to load kms\n");
  263. ret = PTR_ERR(kms);
  264. goto fail;
  265. }
  266. priv->kms = kms;
  267. if (kms) {
  268. pm_runtime_enable(dev->dev);
  269. ret = kms->funcs->hw_init(kms);
  270. if (ret) {
  271. dev_err(dev->dev, "kms hw init failed: %d\n", ret);
  272. goto fail;
  273. }
  274. }
  275. dev->mode_config.min_width = 0;
  276. dev->mode_config.min_height = 0;
  277. dev->mode_config.max_width = 2048;
  278. dev->mode_config.max_height = 2048;
  279. dev->mode_config.funcs = &mode_config_funcs;
  280. ret = drm_vblank_init(dev, priv->num_crtcs);
  281. if (ret < 0) {
  282. dev_err(dev->dev, "failed to initialize vblank\n");
  283. goto fail;
  284. }
  285. pm_runtime_get_sync(dev->dev);
  286. ret = drm_irq_install(dev, platform_get_irq(dev->platformdev, 0));
  287. pm_runtime_put_sync(dev->dev);
  288. if (ret < 0) {
  289. dev_err(dev->dev, "failed to install IRQ handler\n");
  290. goto fail;
  291. }
  292. drm_mode_config_reset(dev);
  293. #ifdef CONFIG_DRM_MSM_FBDEV
  294. if (fbdev)
  295. priv->fbdev = msm_fbdev_init(dev);
  296. #endif
  297. ret = msm_debugfs_late_init(dev);
  298. if (ret)
  299. goto fail;
  300. drm_kms_helper_poll_init(dev);
  301. return 0;
  302. fail:
  303. msm_unload(dev);
  304. return ret;
  305. }
  306. static void load_gpu(struct drm_device *dev)
  307. {
  308. static DEFINE_MUTEX(init_lock);
  309. struct msm_drm_private *priv = dev->dev_private;
  310. mutex_lock(&init_lock);
  311. if (!priv->gpu)
  312. priv->gpu = adreno_load_gpu(dev);
  313. mutex_unlock(&init_lock);
  314. }
  315. static int msm_open(struct drm_device *dev, struct drm_file *file)
  316. {
  317. struct msm_file_private *ctx;
  318. /* For now, load gpu on open.. to avoid the requirement of having
  319. * firmware in the initrd.
  320. */
  321. load_gpu(dev);
  322. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  323. if (!ctx)
  324. return -ENOMEM;
  325. file->driver_priv = ctx;
  326. return 0;
  327. }
  328. static void msm_preclose(struct drm_device *dev, struct drm_file *file)
  329. {
  330. struct msm_drm_private *priv = dev->dev_private;
  331. struct msm_file_private *ctx = file->driver_priv;
  332. struct msm_kms *kms = priv->kms;
  333. if (kms)
  334. kms->funcs->preclose(kms, file);
  335. mutex_lock(&dev->struct_mutex);
  336. if (ctx == priv->lastctx)
  337. priv->lastctx = NULL;
  338. mutex_unlock(&dev->struct_mutex);
  339. kfree(ctx);
  340. }
  341. static void msm_lastclose(struct drm_device *dev)
  342. {
  343. #ifdef CONFIG_DRM_MSM_FBDEV
  344. struct msm_drm_private *priv = dev->dev_private;
  345. if (priv->fbdev)
  346. drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
  347. #endif
  348. }
  349. static irqreturn_t msm_irq(int irq, void *arg)
  350. {
  351. struct drm_device *dev = arg;
  352. struct msm_drm_private *priv = dev->dev_private;
  353. struct msm_kms *kms = priv->kms;
  354. BUG_ON(!kms);
  355. return kms->funcs->irq(kms);
  356. }
  357. static void msm_irq_preinstall(struct drm_device *dev)
  358. {
  359. struct msm_drm_private *priv = dev->dev_private;
  360. struct msm_kms *kms = priv->kms;
  361. BUG_ON(!kms);
  362. kms->funcs->irq_preinstall(kms);
  363. }
  364. static int msm_irq_postinstall(struct drm_device *dev)
  365. {
  366. struct msm_drm_private *priv = dev->dev_private;
  367. struct msm_kms *kms = priv->kms;
  368. BUG_ON(!kms);
  369. return kms->funcs->irq_postinstall(kms);
  370. }
  371. static void msm_irq_uninstall(struct drm_device *dev)
  372. {
  373. struct msm_drm_private *priv = dev->dev_private;
  374. struct msm_kms *kms = priv->kms;
  375. BUG_ON(!kms);
  376. kms->funcs->irq_uninstall(kms);
  377. }
  378. static int msm_enable_vblank(struct drm_device *dev, int crtc_id)
  379. {
  380. struct msm_drm_private *priv = dev->dev_private;
  381. struct msm_kms *kms = priv->kms;
  382. if (!kms)
  383. return -ENXIO;
  384. DBG("dev=%p, crtc=%d", dev, crtc_id);
  385. return kms->funcs->enable_vblank(kms, priv->crtcs[crtc_id]);
  386. }
  387. static void msm_disable_vblank(struct drm_device *dev, int crtc_id)
  388. {
  389. struct msm_drm_private *priv = dev->dev_private;
  390. struct msm_kms *kms = priv->kms;
  391. if (!kms)
  392. return;
  393. DBG("dev=%p, crtc=%d", dev, crtc_id);
  394. kms->funcs->disable_vblank(kms, priv->crtcs[crtc_id]);
  395. }
  396. /*
  397. * DRM debugfs:
  398. */
  399. #ifdef CONFIG_DEBUG_FS
  400. static int msm_gpu_show(struct drm_device *dev, struct seq_file *m)
  401. {
  402. struct msm_drm_private *priv = dev->dev_private;
  403. struct msm_gpu *gpu = priv->gpu;
  404. if (gpu) {
  405. seq_printf(m, "%s Status:\n", gpu->name);
  406. gpu->funcs->show(gpu, m);
  407. }
  408. return 0;
  409. }
  410. static int msm_gem_show(struct drm_device *dev, struct seq_file *m)
  411. {
  412. struct msm_drm_private *priv = dev->dev_private;
  413. struct msm_gpu *gpu = priv->gpu;
  414. if (gpu) {
  415. seq_printf(m, "Active Objects (%s):\n", gpu->name);
  416. msm_gem_describe_objects(&gpu->active_list, m);
  417. }
  418. seq_printf(m, "Inactive Objects:\n");
  419. msm_gem_describe_objects(&priv->inactive_list, m);
  420. return 0;
  421. }
  422. static int msm_mm_show(struct drm_device *dev, struct seq_file *m)
  423. {
  424. return drm_mm_dump_table(m, &dev->vma_offset_manager->vm_addr_space_mm);
  425. }
  426. static int msm_fb_show(struct drm_device *dev, struct seq_file *m)
  427. {
  428. struct msm_drm_private *priv = dev->dev_private;
  429. struct drm_framebuffer *fb, *fbdev_fb = NULL;
  430. if (priv->fbdev) {
  431. seq_printf(m, "fbcon ");
  432. fbdev_fb = priv->fbdev->fb;
  433. msm_framebuffer_describe(fbdev_fb, m);
  434. }
  435. mutex_lock(&dev->mode_config.fb_lock);
  436. list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
  437. if (fb == fbdev_fb)
  438. continue;
  439. seq_printf(m, "user ");
  440. msm_framebuffer_describe(fb, m);
  441. }
  442. mutex_unlock(&dev->mode_config.fb_lock);
  443. return 0;
  444. }
  445. static int show_locked(struct seq_file *m, void *arg)
  446. {
  447. struct drm_info_node *node = (struct drm_info_node *) m->private;
  448. struct drm_device *dev = node->minor->dev;
  449. int (*show)(struct drm_device *dev, struct seq_file *m) =
  450. node->info_ent->data;
  451. int ret;
  452. ret = mutex_lock_interruptible(&dev->struct_mutex);
  453. if (ret)
  454. return ret;
  455. ret = show(dev, m);
  456. mutex_unlock(&dev->struct_mutex);
  457. return ret;
  458. }
  459. static struct drm_info_list msm_debugfs_list[] = {
  460. {"gpu", show_locked, 0, msm_gpu_show},
  461. {"gem", show_locked, 0, msm_gem_show},
  462. { "mm", show_locked, 0, msm_mm_show },
  463. { "fb", show_locked, 0, msm_fb_show },
  464. };
  465. static int late_init_minor(struct drm_minor *minor)
  466. {
  467. int ret;
  468. if (!minor)
  469. return 0;
  470. ret = msm_rd_debugfs_init(minor);
  471. if (ret) {
  472. dev_err(minor->dev->dev, "could not install rd debugfs\n");
  473. return ret;
  474. }
  475. ret = msm_perf_debugfs_init(minor);
  476. if (ret) {
  477. dev_err(minor->dev->dev, "could not install perf debugfs\n");
  478. return ret;
  479. }
  480. return 0;
  481. }
  482. int msm_debugfs_late_init(struct drm_device *dev)
  483. {
  484. int ret;
  485. ret = late_init_minor(dev->primary);
  486. if (ret)
  487. return ret;
  488. ret = late_init_minor(dev->render);
  489. if (ret)
  490. return ret;
  491. ret = late_init_minor(dev->control);
  492. return ret;
  493. }
  494. static int msm_debugfs_init(struct drm_minor *minor)
  495. {
  496. struct drm_device *dev = minor->dev;
  497. int ret;
  498. ret = drm_debugfs_create_files(msm_debugfs_list,
  499. ARRAY_SIZE(msm_debugfs_list),
  500. minor->debugfs_root, minor);
  501. if (ret) {
  502. dev_err(dev->dev, "could not install msm_debugfs_list\n");
  503. return ret;
  504. }
  505. return 0;
  506. }
  507. static void msm_debugfs_cleanup(struct drm_minor *minor)
  508. {
  509. drm_debugfs_remove_files(msm_debugfs_list,
  510. ARRAY_SIZE(msm_debugfs_list), minor);
  511. if (!minor->dev->dev_private)
  512. return;
  513. msm_rd_debugfs_cleanup(minor);
  514. msm_perf_debugfs_cleanup(minor);
  515. }
  516. #endif
  517. /*
  518. * Fences:
  519. */
  520. int msm_wait_fence_interruptable(struct drm_device *dev, uint32_t fence,
  521. struct timespec *timeout)
  522. {
  523. struct msm_drm_private *priv = dev->dev_private;
  524. int ret;
  525. if (!priv->gpu)
  526. return 0;
  527. if (fence > priv->gpu->submitted_fence) {
  528. DRM_ERROR("waiting on invalid fence: %u (of %u)\n",
  529. fence, priv->gpu->submitted_fence);
  530. return -EINVAL;
  531. }
  532. if (!timeout) {
  533. /* no-wait: */
  534. ret = fence_completed(dev, fence) ? 0 : -EBUSY;
  535. } else {
  536. unsigned long timeout_jiffies = timespec_to_jiffies(timeout);
  537. unsigned long start_jiffies = jiffies;
  538. unsigned long remaining_jiffies;
  539. if (time_after(start_jiffies, timeout_jiffies))
  540. remaining_jiffies = 0;
  541. else
  542. remaining_jiffies = timeout_jiffies - start_jiffies;
  543. ret = wait_event_interruptible_timeout(priv->fence_event,
  544. fence_completed(dev, fence),
  545. remaining_jiffies);
  546. if (ret == 0) {
  547. DBG("timeout waiting for fence: %u (completed: %u)",
  548. fence, priv->completed_fence);
  549. ret = -ETIMEDOUT;
  550. } else if (ret != -ERESTARTSYS) {
  551. ret = 0;
  552. }
  553. }
  554. return ret;
  555. }
  556. int msm_queue_fence_cb(struct drm_device *dev,
  557. struct msm_fence_cb *cb, uint32_t fence)
  558. {
  559. struct msm_drm_private *priv = dev->dev_private;
  560. int ret = 0;
  561. mutex_lock(&dev->struct_mutex);
  562. if (!list_empty(&cb->work.entry)) {
  563. ret = -EINVAL;
  564. } else if (fence > priv->completed_fence) {
  565. cb->fence = fence;
  566. list_add_tail(&cb->work.entry, &priv->fence_cbs);
  567. } else {
  568. queue_work(priv->wq, &cb->work);
  569. }
  570. mutex_unlock(&dev->struct_mutex);
  571. return ret;
  572. }
  573. /* called from workqueue */
  574. void msm_update_fence(struct drm_device *dev, uint32_t fence)
  575. {
  576. struct msm_drm_private *priv = dev->dev_private;
  577. mutex_lock(&dev->struct_mutex);
  578. priv->completed_fence = max(fence, priv->completed_fence);
  579. while (!list_empty(&priv->fence_cbs)) {
  580. struct msm_fence_cb *cb;
  581. cb = list_first_entry(&priv->fence_cbs,
  582. struct msm_fence_cb, work.entry);
  583. if (cb->fence > priv->completed_fence)
  584. break;
  585. list_del_init(&cb->work.entry);
  586. queue_work(priv->wq, &cb->work);
  587. }
  588. mutex_unlock(&dev->struct_mutex);
  589. wake_up_all(&priv->fence_event);
  590. }
  591. void __msm_fence_worker(struct work_struct *work)
  592. {
  593. struct msm_fence_cb *cb = container_of(work, struct msm_fence_cb, work);
  594. cb->func(cb);
  595. }
  596. /*
  597. * DRM ioctls:
  598. */
  599. static int msm_ioctl_get_param(struct drm_device *dev, void *data,
  600. struct drm_file *file)
  601. {
  602. struct msm_drm_private *priv = dev->dev_private;
  603. struct drm_msm_param *args = data;
  604. struct msm_gpu *gpu;
  605. /* for now, we just have 3d pipe.. eventually this would need to
  606. * be more clever to dispatch to appropriate gpu module:
  607. */
  608. if (args->pipe != MSM_PIPE_3D0)
  609. return -EINVAL;
  610. gpu = priv->gpu;
  611. if (!gpu)
  612. return -ENXIO;
  613. return gpu->funcs->get_param(gpu, args->param, &args->value);
  614. }
  615. static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
  616. struct drm_file *file)
  617. {
  618. struct drm_msm_gem_new *args = data;
  619. if (args->flags & ~MSM_BO_FLAGS) {
  620. DRM_ERROR("invalid flags: %08x\n", args->flags);
  621. return -EINVAL;
  622. }
  623. return msm_gem_new_handle(dev, file, args->size,
  624. args->flags, &args->handle);
  625. }
  626. #define TS(t) ((struct timespec){ .tv_sec = (t).tv_sec, .tv_nsec = (t).tv_nsec })
  627. static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
  628. struct drm_file *file)
  629. {
  630. struct drm_msm_gem_cpu_prep *args = data;
  631. struct drm_gem_object *obj;
  632. int ret;
  633. if (args->op & ~MSM_PREP_FLAGS) {
  634. DRM_ERROR("invalid op: %08x\n", args->op);
  635. return -EINVAL;
  636. }
  637. obj = drm_gem_object_lookup(dev, file, args->handle);
  638. if (!obj)
  639. return -ENOENT;
  640. ret = msm_gem_cpu_prep(obj, args->op, &TS(args->timeout));
  641. drm_gem_object_unreference_unlocked(obj);
  642. return ret;
  643. }
  644. static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
  645. struct drm_file *file)
  646. {
  647. struct drm_msm_gem_cpu_fini *args = data;
  648. struct drm_gem_object *obj;
  649. int ret;
  650. obj = drm_gem_object_lookup(dev, file, args->handle);
  651. if (!obj)
  652. return -ENOENT;
  653. ret = msm_gem_cpu_fini(obj);
  654. drm_gem_object_unreference_unlocked(obj);
  655. return ret;
  656. }
  657. static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
  658. struct drm_file *file)
  659. {
  660. struct drm_msm_gem_info *args = data;
  661. struct drm_gem_object *obj;
  662. int ret = 0;
  663. if (args->pad)
  664. return -EINVAL;
  665. obj = drm_gem_object_lookup(dev, file, args->handle);
  666. if (!obj)
  667. return -ENOENT;
  668. args->offset = msm_gem_mmap_offset(obj);
  669. drm_gem_object_unreference_unlocked(obj);
  670. return ret;
  671. }
  672. static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
  673. struct drm_file *file)
  674. {
  675. struct drm_msm_wait_fence *args = data;
  676. if (args->pad) {
  677. DRM_ERROR("invalid pad: %08x\n", args->pad);
  678. return -EINVAL;
  679. }
  680. return msm_wait_fence_interruptable(dev, args->fence,
  681. &TS(args->timeout));
  682. }
  683. static const struct drm_ioctl_desc msm_ioctls[] = {
  684. DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  685. DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  686. DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  687. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  688. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  689. DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  690. DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  691. };
  692. static const struct vm_operations_struct vm_ops = {
  693. .fault = msm_gem_fault,
  694. .open = drm_gem_vm_open,
  695. .close = drm_gem_vm_close,
  696. };
  697. static const struct file_operations fops = {
  698. .owner = THIS_MODULE,
  699. .open = drm_open,
  700. .release = drm_release,
  701. .unlocked_ioctl = drm_ioctl,
  702. #ifdef CONFIG_COMPAT
  703. .compat_ioctl = drm_compat_ioctl,
  704. #endif
  705. .poll = drm_poll,
  706. .read = drm_read,
  707. .llseek = no_llseek,
  708. .mmap = msm_gem_mmap,
  709. };
  710. static struct drm_driver msm_driver = {
  711. .driver_features = DRIVER_HAVE_IRQ |
  712. DRIVER_GEM |
  713. DRIVER_PRIME |
  714. DRIVER_RENDER |
  715. DRIVER_MODESET,
  716. .load = msm_load,
  717. .unload = msm_unload,
  718. .open = msm_open,
  719. .preclose = msm_preclose,
  720. .lastclose = msm_lastclose,
  721. .set_busid = drm_platform_set_busid,
  722. .irq_handler = msm_irq,
  723. .irq_preinstall = msm_irq_preinstall,
  724. .irq_postinstall = msm_irq_postinstall,
  725. .irq_uninstall = msm_irq_uninstall,
  726. .get_vblank_counter = drm_vblank_count,
  727. .enable_vblank = msm_enable_vblank,
  728. .disable_vblank = msm_disable_vblank,
  729. .gem_free_object = msm_gem_free_object,
  730. .gem_vm_ops = &vm_ops,
  731. .dumb_create = msm_gem_dumb_create,
  732. .dumb_map_offset = msm_gem_dumb_map_offset,
  733. .dumb_destroy = drm_gem_dumb_destroy,
  734. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  735. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  736. .gem_prime_export = drm_gem_prime_export,
  737. .gem_prime_import = drm_gem_prime_import,
  738. .gem_prime_pin = msm_gem_prime_pin,
  739. .gem_prime_unpin = msm_gem_prime_unpin,
  740. .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
  741. .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
  742. .gem_prime_vmap = msm_gem_prime_vmap,
  743. .gem_prime_vunmap = msm_gem_prime_vunmap,
  744. .gem_prime_mmap = msm_gem_prime_mmap,
  745. #ifdef CONFIG_DEBUG_FS
  746. .debugfs_init = msm_debugfs_init,
  747. .debugfs_cleanup = msm_debugfs_cleanup,
  748. #endif
  749. .ioctls = msm_ioctls,
  750. .num_ioctls = DRM_MSM_NUM_IOCTLS,
  751. .fops = &fops,
  752. .name = "msm",
  753. .desc = "MSM Snapdragon DRM",
  754. .date = "20130625",
  755. .major = 1,
  756. .minor = 0,
  757. };
  758. #ifdef CONFIG_PM_SLEEP
  759. static int msm_pm_suspend(struct device *dev)
  760. {
  761. struct drm_device *ddev = dev_get_drvdata(dev);
  762. drm_kms_helper_poll_disable(ddev);
  763. return 0;
  764. }
  765. static int msm_pm_resume(struct device *dev)
  766. {
  767. struct drm_device *ddev = dev_get_drvdata(dev);
  768. drm_kms_helper_poll_enable(ddev);
  769. return 0;
  770. }
  771. #endif
  772. static const struct dev_pm_ops msm_pm_ops = {
  773. SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
  774. };
  775. /*
  776. * Componentized driver support:
  777. */
  778. #ifdef CONFIG_OF
  779. /* NOTE: the CONFIG_OF case duplicates the same code as exynos or imx
  780. * (or probably any other).. so probably some room for some helpers
  781. */
  782. static int compare_of(struct device *dev, void *data)
  783. {
  784. return dev->of_node == data;
  785. }
  786. static int add_components(struct device *dev, struct component_match **matchptr,
  787. const char *name)
  788. {
  789. struct device_node *np = dev->of_node;
  790. unsigned i;
  791. for (i = 0; ; i++) {
  792. struct device_node *node;
  793. node = of_parse_phandle(np, name, i);
  794. if (!node)
  795. break;
  796. component_match_add(dev, matchptr, compare_of, node);
  797. }
  798. return 0;
  799. }
  800. #else
  801. static int compare_dev(struct device *dev, void *data)
  802. {
  803. return dev == data;
  804. }
  805. #endif
  806. static int msm_drm_bind(struct device *dev)
  807. {
  808. return drm_platform_init(&msm_driver, to_platform_device(dev));
  809. }
  810. static void msm_drm_unbind(struct device *dev)
  811. {
  812. drm_put_dev(platform_get_drvdata(to_platform_device(dev)));
  813. }
  814. static const struct component_master_ops msm_drm_ops = {
  815. .bind = msm_drm_bind,
  816. .unbind = msm_drm_unbind,
  817. };
  818. /*
  819. * Platform driver:
  820. */
  821. static int msm_pdev_probe(struct platform_device *pdev)
  822. {
  823. struct component_match *match = NULL;
  824. #ifdef CONFIG_OF
  825. add_components(&pdev->dev, &match, "connectors");
  826. add_components(&pdev->dev, &match, "gpus");
  827. #else
  828. /* For non-DT case, it kinda sucks. We don't actually have a way
  829. * to know whether or not we are waiting for certain devices (or if
  830. * they are simply not present). But for non-DT we only need to
  831. * care about apq8064/apq8060/etc (all mdp4/a3xx):
  832. */
  833. static const char *devnames[] = {
  834. "hdmi_msm.0", "kgsl-3d0.0",
  835. };
  836. int i;
  837. DBG("Adding components..");
  838. for (i = 0; i < ARRAY_SIZE(devnames); i++) {
  839. struct device *dev;
  840. dev = bus_find_device_by_name(&platform_bus_type,
  841. NULL, devnames[i]);
  842. if (!dev) {
  843. dev_info(&pdev->dev, "still waiting for %s\n", devnames[i]);
  844. return -EPROBE_DEFER;
  845. }
  846. component_match_add(&pdev->dev, &match, compare_dev, dev);
  847. }
  848. #endif
  849. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  850. return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
  851. }
  852. static int msm_pdev_remove(struct platform_device *pdev)
  853. {
  854. component_master_del(&pdev->dev, &msm_drm_ops);
  855. return 0;
  856. }
  857. static const struct platform_device_id msm_id[] = {
  858. { "mdp", 0 },
  859. { }
  860. };
  861. static const struct of_device_id dt_match[] = {
  862. { .compatible = "qcom,mdp" }, /* mdp4 */
  863. { .compatible = "qcom,mdss_mdp" }, /* mdp5 */
  864. {}
  865. };
  866. MODULE_DEVICE_TABLE(of, dt_match);
  867. static struct platform_driver msm_platform_driver = {
  868. .probe = msm_pdev_probe,
  869. .remove = msm_pdev_remove,
  870. .driver = {
  871. .name = "msm",
  872. .of_match_table = dt_match,
  873. .pm = &msm_pm_ops,
  874. },
  875. .id_table = msm_id,
  876. };
  877. static int __init msm_drm_register(void)
  878. {
  879. DBG("init");
  880. msm_dsi_register();
  881. msm_edp_register();
  882. hdmi_register();
  883. adreno_register();
  884. return platform_driver_register(&msm_platform_driver);
  885. }
  886. static void __exit msm_drm_unregister(void)
  887. {
  888. DBG("fini");
  889. platform_driver_unregister(&msm_platform_driver);
  890. hdmi_unregister();
  891. adreno_unregister();
  892. msm_edp_unregister();
  893. msm_dsi_unregister();
  894. }
  895. module_init(msm_drm_register);
  896. module_exit(msm_drm_unregister);
  897. MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
  898. MODULE_DESCRIPTION("MSM DRM Driver");
  899. MODULE_LICENSE("GPL");