msm_drv.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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_debugfs.h"
  19. #include "msm_fence.h"
  20. #include "msm_gpu.h"
  21. #include "msm_kms.h"
  22. /*
  23. * MSM driver version:
  24. * - 1.0.0 - initial interface
  25. * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers
  26. */
  27. #define MSM_VERSION_MAJOR 1
  28. #define MSM_VERSION_MINOR 1
  29. #define MSM_VERSION_PATCHLEVEL 0
  30. static void msm_fb_output_poll_changed(struct drm_device *dev)
  31. {
  32. struct msm_drm_private *priv = dev->dev_private;
  33. if (priv->fbdev)
  34. drm_fb_helper_hotplug_event(priv->fbdev);
  35. }
  36. static const struct drm_mode_config_funcs mode_config_funcs = {
  37. .fb_create = msm_framebuffer_create,
  38. .output_poll_changed = msm_fb_output_poll_changed,
  39. .atomic_check = msm_atomic_check,
  40. .atomic_commit = msm_atomic_commit,
  41. };
  42. int msm_register_mmu(struct drm_device *dev, struct msm_mmu *mmu)
  43. {
  44. struct msm_drm_private *priv = dev->dev_private;
  45. int idx = priv->num_mmus++;
  46. if (WARN_ON(idx >= ARRAY_SIZE(priv->mmus)))
  47. return -EINVAL;
  48. priv->mmus[idx] = mmu;
  49. return idx;
  50. }
  51. #ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
  52. static bool reglog = false;
  53. MODULE_PARM_DESC(reglog, "Enable register read/write logging");
  54. module_param(reglog, bool, 0600);
  55. #else
  56. #define reglog 0
  57. #endif
  58. #ifdef CONFIG_DRM_FBDEV_EMULATION
  59. static bool fbdev = true;
  60. MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
  61. module_param(fbdev, bool, 0600);
  62. #endif
  63. static char *vram = "16m";
  64. MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
  65. module_param(vram, charp, 0);
  66. /*
  67. * Util/helpers:
  68. */
  69. void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
  70. const char *dbgname)
  71. {
  72. struct resource *res;
  73. unsigned long size;
  74. void __iomem *ptr;
  75. if (name)
  76. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  77. else
  78. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  79. if (!res) {
  80. dev_err(&pdev->dev, "failed to get memory resource: %s\n", name);
  81. return ERR_PTR(-EINVAL);
  82. }
  83. size = resource_size(res);
  84. ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
  85. if (!ptr) {
  86. dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
  87. return ERR_PTR(-ENOMEM);
  88. }
  89. if (reglog)
  90. printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
  91. return ptr;
  92. }
  93. void msm_writel(u32 data, void __iomem *addr)
  94. {
  95. if (reglog)
  96. printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
  97. writel(data, addr);
  98. }
  99. u32 msm_readl(const void __iomem *addr)
  100. {
  101. u32 val = readl(addr);
  102. if (reglog)
  103. printk(KERN_ERR "IO:R %p %08x\n", addr, val);
  104. return val;
  105. }
  106. struct vblank_event {
  107. struct list_head node;
  108. int crtc_id;
  109. bool enable;
  110. };
  111. static void vblank_ctrl_worker(struct work_struct *work)
  112. {
  113. struct msm_vblank_ctrl *vbl_ctrl = container_of(work,
  114. struct msm_vblank_ctrl, work);
  115. struct msm_drm_private *priv = container_of(vbl_ctrl,
  116. struct msm_drm_private, vblank_ctrl);
  117. struct msm_kms *kms = priv->kms;
  118. struct vblank_event *vbl_ev, *tmp;
  119. unsigned long flags;
  120. spin_lock_irqsave(&vbl_ctrl->lock, flags);
  121. list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
  122. list_del(&vbl_ev->node);
  123. spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
  124. if (vbl_ev->enable)
  125. kms->funcs->enable_vblank(kms,
  126. priv->crtcs[vbl_ev->crtc_id]);
  127. else
  128. kms->funcs->disable_vblank(kms,
  129. priv->crtcs[vbl_ev->crtc_id]);
  130. kfree(vbl_ev);
  131. spin_lock_irqsave(&vbl_ctrl->lock, flags);
  132. }
  133. spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
  134. }
  135. static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
  136. int crtc_id, bool enable)
  137. {
  138. struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
  139. struct vblank_event *vbl_ev;
  140. unsigned long flags;
  141. vbl_ev = kzalloc(sizeof(*vbl_ev), GFP_ATOMIC);
  142. if (!vbl_ev)
  143. return -ENOMEM;
  144. vbl_ev->crtc_id = crtc_id;
  145. vbl_ev->enable = enable;
  146. spin_lock_irqsave(&vbl_ctrl->lock, flags);
  147. list_add_tail(&vbl_ev->node, &vbl_ctrl->event_list);
  148. spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
  149. queue_work(priv->wq, &vbl_ctrl->work);
  150. return 0;
  151. }
  152. static int msm_drm_uninit(struct device *dev)
  153. {
  154. struct platform_device *pdev = to_platform_device(dev);
  155. struct drm_device *ddev = platform_get_drvdata(pdev);
  156. struct msm_drm_private *priv = ddev->dev_private;
  157. struct msm_kms *kms = priv->kms;
  158. struct msm_gpu *gpu = priv->gpu;
  159. struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
  160. struct vblank_event *vbl_ev, *tmp;
  161. /* We must cancel and cleanup any pending vblank enable/disable
  162. * work before drm_irq_uninstall() to avoid work re-enabling an
  163. * irq after uninstall has disabled it.
  164. */
  165. cancel_work_sync(&vbl_ctrl->work);
  166. list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
  167. list_del(&vbl_ev->node);
  168. kfree(vbl_ev);
  169. }
  170. msm_gem_shrinker_cleanup(ddev);
  171. drm_kms_helper_poll_fini(ddev);
  172. drm_dev_unregister(ddev);
  173. #ifdef CONFIG_DRM_FBDEV_EMULATION
  174. if (fbdev && priv->fbdev)
  175. msm_fbdev_free(ddev);
  176. #endif
  177. drm_mode_config_cleanup(ddev);
  178. pm_runtime_get_sync(dev);
  179. drm_irq_uninstall(ddev);
  180. pm_runtime_put_sync(dev);
  181. flush_workqueue(priv->wq);
  182. destroy_workqueue(priv->wq);
  183. flush_workqueue(priv->atomic_wq);
  184. destroy_workqueue(priv->atomic_wq);
  185. if (kms)
  186. kms->funcs->destroy(kms);
  187. if (gpu) {
  188. mutex_lock(&ddev->struct_mutex);
  189. gpu->funcs->pm_suspend(gpu);
  190. mutex_unlock(&ddev->struct_mutex);
  191. gpu->funcs->destroy(gpu);
  192. }
  193. if (priv->vram.paddr) {
  194. unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
  195. drm_mm_takedown(&priv->vram.mm);
  196. dma_free_attrs(dev, priv->vram.size, NULL,
  197. priv->vram.paddr, attrs);
  198. }
  199. component_unbind_all(dev, ddev);
  200. msm_mdss_destroy(ddev);
  201. ddev->dev_private = NULL;
  202. drm_dev_unref(ddev);
  203. kfree(priv);
  204. return 0;
  205. }
  206. static int get_mdp_ver(struct platform_device *pdev)
  207. {
  208. struct device *dev = &pdev->dev;
  209. return (int) (unsigned long) of_device_get_match_data(dev);
  210. }
  211. #include <linux/of_address.h>
  212. static int msm_init_vram(struct drm_device *dev)
  213. {
  214. struct msm_drm_private *priv = dev->dev_private;
  215. struct device_node *node;
  216. unsigned long size = 0;
  217. int ret = 0;
  218. /* In the device-tree world, we could have a 'memory-region'
  219. * phandle, which gives us a link to our "vram". Allocating
  220. * is all nicely abstracted behind the dma api, but we need
  221. * to know the entire size to allocate it all in one go. There
  222. * are two cases:
  223. * 1) device with no IOMMU, in which case we need exclusive
  224. * access to a VRAM carveout big enough for all gpu
  225. * buffers
  226. * 2) device with IOMMU, but where the bootloader puts up
  227. * a splash screen. In this case, the VRAM carveout
  228. * need only be large enough for fbdev fb. But we need
  229. * exclusive access to the buffer to avoid the kernel
  230. * using those pages for other purposes (which appears
  231. * as corruption on screen before we have a chance to
  232. * load and do initial modeset)
  233. */
  234. node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
  235. if (node) {
  236. struct resource r;
  237. ret = of_address_to_resource(node, 0, &r);
  238. of_node_put(node);
  239. if (ret)
  240. return ret;
  241. size = r.end - r.start;
  242. DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
  243. /* if we have no IOMMU, then we need to use carveout allocator.
  244. * Grab the entire CMA chunk carved out in early startup in
  245. * mach-msm:
  246. */
  247. } else if (!iommu_present(&platform_bus_type)) {
  248. DRM_INFO("using %s VRAM carveout\n", vram);
  249. size = memparse(vram, NULL);
  250. }
  251. if (size) {
  252. unsigned long attrs = 0;
  253. void *p;
  254. priv->vram.size = size;
  255. drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
  256. attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
  257. attrs |= DMA_ATTR_WRITE_COMBINE;
  258. /* note that for no-kernel-mapping, the vaddr returned
  259. * is bogus, but non-null if allocation succeeded:
  260. */
  261. p = dma_alloc_attrs(dev->dev, size,
  262. &priv->vram.paddr, GFP_KERNEL, attrs);
  263. if (!p) {
  264. dev_err(dev->dev, "failed to allocate VRAM\n");
  265. priv->vram.paddr = 0;
  266. return -ENOMEM;
  267. }
  268. dev_info(dev->dev, "VRAM: %08x->%08x\n",
  269. (uint32_t)priv->vram.paddr,
  270. (uint32_t)(priv->vram.paddr + size));
  271. }
  272. return ret;
  273. }
  274. static int msm_drm_init(struct device *dev, struct drm_driver *drv)
  275. {
  276. struct platform_device *pdev = to_platform_device(dev);
  277. struct drm_device *ddev;
  278. struct msm_drm_private *priv;
  279. struct msm_kms *kms;
  280. int ret;
  281. ddev = drm_dev_alloc(drv, dev);
  282. if (!ddev) {
  283. dev_err(dev, "failed to allocate drm_device\n");
  284. return -ENOMEM;
  285. }
  286. platform_set_drvdata(pdev, ddev);
  287. ddev->platformdev = pdev;
  288. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  289. if (!priv) {
  290. drm_dev_unref(ddev);
  291. return -ENOMEM;
  292. }
  293. ddev->dev_private = priv;
  294. priv->dev = ddev;
  295. ret = msm_mdss_init(ddev);
  296. if (ret) {
  297. kfree(priv);
  298. drm_dev_unref(ddev);
  299. return ret;
  300. }
  301. priv->wq = alloc_ordered_workqueue("msm", 0);
  302. priv->atomic_wq = alloc_ordered_workqueue("msm:atomic", 0);
  303. init_waitqueue_head(&priv->pending_crtcs_event);
  304. INIT_LIST_HEAD(&priv->inactive_list);
  305. INIT_LIST_HEAD(&priv->vblank_ctrl.event_list);
  306. INIT_WORK(&priv->vblank_ctrl.work, vblank_ctrl_worker);
  307. spin_lock_init(&priv->vblank_ctrl.lock);
  308. drm_mode_config_init(ddev);
  309. /* Bind all our sub-components: */
  310. ret = component_bind_all(dev, ddev);
  311. if (ret) {
  312. msm_mdss_destroy(ddev);
  313. kfree(priv);
  314. drm_dev_unref(ddev);
  315. return ret;
  316. }
  317. ret = msm_init_vram(ddev);
  318. if (ret)
  319. goto fail;
  320. msm_gem_shrinker_init(ddev);
  321. switch (get_mdp_ver(pdev)) {
  322. case 4:
  323. kms = mdp4_kms_init(ddev);
  324. priv->kms = kms;
  325. break;
  326. case 5:
  327. kms = mdp5_kms_init(ddev);
  328. break;
  329. default:
  330. kms = ERR_PTR(-ENODEV);
  331. break;
  332. }
  333. if (IS_ERR(kms)) {
  334. /*
  335. * NOTE: once we have GPU support, having no kms should not
  336. * be considered fatal.. ideally we would still support gpu
  337. * and (for example) use dmabuf/prime to share buffers with
  338. * imx drm driver on iMX5
  339. */
  340. dev_err(dev, "failed to load kms\n");
  341. ret = PTR_ERR(kms);
  342. goto fail;
  343. }
  344. if (kms) {
  345. ret = kms->funcs->hw_init(kms);
  346. if (ret) {
  347. dev_err(dev, "kms hw init failed: %d\n", ret);
  348. goto fail;
  349. }
  350. }
  351. ddev->mode_config.funcs = &mode_config_funcs;
  352. ret = drm_vblank_init(ddev, priv->num_crtcs);
  353. if (ret < 0) {
  354. dev_err(dev, "failed to initialize vblank\n");
  355. goto fail;
  356. }
  357. if (kms) {
  358. pm_runtime_get_sync(dev);
  359. ret = drm_irq_install(ddev, kms->irq);
  360. pm_runtime_put_sync(dev);
  361. if (ret < 0) {
  362. dev_err(dev, "failed to install IRQ handler\n");
  363. goto fail;
  364. }
  365. }
  366. ret = drm_dev_register(ddev, 0);
  367. if (ret)
  368. goto fail;
  369. drm_mode_config_reset(ddev);
  370. #ifdef CONFIG_DRM_FBDEV_EMULATION
  371. if (fbdev)
  372. priv->fbdev = msm_fbdev_init(ddev);
  373. #endif
  374. ret = msm_debugfs_late_init(ddev);
  375. if (ret)
  376. goto fail;
  377. drm_kms_helper_poll_init(ddev);
  378. return 0;
  379. fail:
  380. msm_drm_uninit(dev);
  381. return ret;
  382. }
  383. /*
  384. * DRM operations:
  385. */
  386. static void load_gpu(struct drm_device *dev)
  387. {
  388. static DEFINE_MUTEX(init_lock);
  389. struct msm_drm_private *priv = dev->dev_private;
  390. mutex_lock(&init_lock);
  391. if (!priv->gpu)
  392. priv->gpu = adreno_load_gpu(dev);
  393. mutex_unlock(&init_lock);
  394. }
  395. static int msm_open(struct drm_device *dev, struct drm_file *file)
  396. {
  397. struct msm_file_private *ctx;
  398. /* For now, load gpu on open.. to avoid the requirement of having
  399. * firmware in the initrd.
  400. */
  401. load_gpu(dev);
  402. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  403. if (!ctx)
  404. return -ENOMEM;
  405. file->driver_priv = ctx;
  406. return 0;
  407. }
  408. static void msm_preclose(struct drm_device *dev, struct drm_file *file)
  409. {
  410. struct msm_drm_private *priv = dev->dev_private;
  411. struct msm_file_private *ctx = file->driver_priv;
  412. mutex_lock(&dev->struct_mutex);
  413. if (ctx == priv->lastctx)
  414. priv->lastctx = NULL;
  415. mutex_unlock(&dev->struct_mutex);
  416. kfree(ctx);
  417. }
  418. static void msm_lastclose(struct drm_device *dev)
  419. {
  420. struct msm_drm_private *priv = dev->dev_private;
  421. if (priv->fbdev)
  422. drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
  423. }
  424. static irqreturn_t msm_irq(int irq, void *arg)
  425. {
  426. struct drm_device *dev = arg;
  427. struct msm_drm_private *priv = dev->dev_private;
  428. struct msm_kms *kms = priv->kms;
  429. BUG_ON(!kms);
  430. return kms->funcs->irq(kms);
  431. }
  432. static void msm_irq_preinstall(struct drm_device *dev)
  433. {
  434. struct msm_drm_private *priv = dev->dev_private;
  435. struct msm_kms *kms = priv->kms;
  436. BUG_ON(!kms);
  437. kms->funcs->irq_preinstall(kms);
  438. }
  439. static int msm_irq_postinstall(struct drm_device *dev)
  440. {
  441. struct msm_drm_private *priv = dev->dev_private;
  442. struct msm_kms *kms = priv->kms;
  443. BUG_ON(!kms);
  444. return kms->funcs->irq_postinstall(kms);
  445. }
  446. static void msm_irq_uninstall(struct drm_device *dev)
  447. {
  448. struct msm_drm_private *priv = dev->dev_private;
  449. struct msm_kms *kms = priv->kms;
  450. BUG_ON(!kms);
  451. kms->funcs->irq_uninstall(kms);
  452. }
  453. static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe)
  454. {
  455. struct msm_drm_private *priv = dev->dev_private;
  456. struct msm_kms *kms = priv->kms;
  457. if (!kms)
  458. return -ENXIO;
  459. DBG("dev=%p, crtc=%u", dev, pipe);
  460. return vblank_ctrl_queue_work(priv, pipe, true);
  461. }
  462. static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe)
  463. {
  464. struct msm_drm_private *priv = dev->dev_private;
  465. struct msm_kms *kms = priv->kms;
  466. if (!kms)
  467. return;
  468. DBG("dev=%p, crtc=%u", dev, pipe);
  469. vblank_ctrl_queue_work(priv, pipe, false);
  470. }
  471. /*
  472. * DRM ioctls:
  473. */
  474. static int msm_ioctl_get_param(struct drm_device *dev, void *data,
  475. struct drm_file *file)
  476. {
  477. struct msm_drm_private *priv = dev->dev_private;
  478. struct drm_msm_param *args = data;
  479. struct msm_gpu *gpu;
  480. /* for now, we just have 3d pipe.. eventually this would need to
  481. * be more clever to dispatch to appropriate gpu module:
  482. */
  483. if (args->pipe != MSM_PIPE_3D0)
  484. return -EINVAL;
  485. gpu = priv->gpu;
  486. if (!gpu)
  487. return -ENXIO;
  488. return gpu->funcs->get_param(gpu, args->param, &args->value);
  489. }
  490. static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
  491. struct drm_file *file)
  492. {
  493. struct drm_msm_gem_new *args = data;
  494. if (args->flags & ~MSM_BO_FLAGS) {
  495. DRM_ERROR("invalid flags: %08x\n", args->flags);
  496. return -EINVAL;
  497. }
  498. return msm_gem_new_handle(dev, file, args->size,
  499. args->flags, &args->handle);
  500. }
  501. static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
  502. {
  503. return ktime_set(timeout.tv_sec, timeout.tv_nsec);
  504. }
  505. static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
  506. struct drm_file *file)
  507. {
  508. struct drm_msm_gem_cpu_prep *args = data;
  509. struct drm_gem_object *obj;
  510. ktime_t timeout = to_ktime(args->timeout);
  511. int ret;
  512. if (args->op & ~MSM_PREP_FLAGS) {
  513. DRM_ERROR("invalid op: %08x\n", args->op);
  514. return -EINVAL;
  515. }
  516. obj = drm_gem_object_lookup(file, args->handle);
  517. if (!obj)
  518. return -ENOENT;
  519. ret = msm_gem_cpu_prep(obj, args->op, &timeout);
  520. drm_gem_object_unreference_unlocked(obj);
  521. return ret;
  522. }
  523. static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
  524. struct drm_file *file)
  525. {
  526. struct drm_msm_gem_cpu_fini *args = data;
  527. struct drm_gem_object *obj;
  528. int ret;
  529. obj = drm_gem_object_lookup(file, args->handle);
  530. if (!obj)
  531. return -ENOENT;
  532. ret = msm_gem_cpu_fini(obj);
  533. drm_gem_object_unreference_unlocked(obj);
  534. return ret;
  535. }
  536. static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
  537. struct drm_file *file)
  538. {
  539. struct drm_msm_gem_info *args = data;
  540. struct drm_gem_object *obj;
  541. int ret = 0;
  542. if (args->pad)
  543. return -EINVAL;
  544. obj = drm_gem_object_lookup(file, args->handle);
  545. if (!obj)
  546. return -ENOENT;
  547. args->offset = msm_gem_mmap_offset(obj);
  548. drm_gem_object_unreference_unlocked(obj);
  549. return ret;
  550. }
  551. static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
  552. struct drm_file *file)
  553. {
  554. struct msm_drm_private *priv = dev->dev_private;
  555. struct drm_msm_wait_fence *args = data;
  556. ktime_t timeout = to_ktime(args->timeout);
  557. if (args->pad) {
  558. DRM_ERROR("invalid pad: %08x\n", args->pad);
  559. return -EINVAL;
  560. }
  561. if (!priv->gpu)
  562. return 0;
  563. return msm_wait_fence(priv->gpu->fctx, args->fence, &timeout, true);
  564. }
  565. static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
  566. struct drm_file *file)
  567. {
  568. struct drm_msm_gem_madvise *args = data;
  569. struct drm_gem_object *obj;
  570. int ret;
  571. switch (args->madv) {
  572. case MSM_MADV_DONTNEED:
  573. case MSM_MADV_WILLNEED:
  574. break;
  575. default:
  576. return -EINVAL;
  577. }
  578. ret = mutex_lock_interruptible(&dev->struct_mutex);
  579. if (ret)
  580. return ret;
  581. obj = drm_gem_object_lookup(file, args->handle);
  582. if (!obj) {
  583. ret = -ENOENT;
  584. goto unlock;
  585. }
  586. ret = msm_gem_madvise(obj, args->madv);
  587. if (ret >= 0) {
  588. args->retained = ret;
  589. ret = 0;
  590. }
  591. drm_gem_object_unreference(obj);
  592. unlock:
  593. mutex_unlock(&dev->struct_mutex);
  594. return ret;
  595. }
  596. static const struct drm_ioctl_desc msm_ioctls[] = {
  597. DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_AUTH|DRM_RENDER_ALLOW),
  598. DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_AUTH|DRM_RENDER_ALLOW),
  599. DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_AUTH|DRM_RENDER_ALLOW),
  600. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
  601. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
  602. DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_AUTH|DRM_RENDER_ALLOW),
  603. DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_AUTH|DRM_RENDER_ALLOW),
  604. DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE, msm_ioctl_gem_madvise, DRM_AUTH|DRM_RENDER_ALLOW),
  605. };
  606. static const struct vm_operations_struct vm_ops = {
  607. .fault = msm_gem_fault,
  608. .open = drm_gem_vm_open,
  609. .close = drm_gem_vm_close,
  610. };
  611. static const struct file_operations fops = {
  612. .owner = THIS_MODULE,
  613. .open = drm_open,
  614. .release = drm_release,
  615. .unlocked_ioctl = drm_ioctl,
  616. #ifdef CONFIG_COMPAT
  617. .compat_ioctl = drm_compat_ioctl,
  618. #endif
  619. .poll = drm_poll,
  620. .read = drm_read,
  621. .llseek = no_llseek,
  622. .mmap = msm_gem_mmap,
  623. };
  624. static struct drm_driver msm_driver = {
  625. .driver_features = DRIVER_HAVE_IRQ |
  626. DRIVER_GEM |
  627. DRIVER_PRIME |
  628. DRIVER_RENDER |
  629. DRIVER_ATOMIC |
  630. DRIVER_MODESET,
  631. .open = msm_open,
  632. .preclose = msm_preclose,
  633. .lastclose = msm_lastclose,
  634. .irq_handler = msm_irq,
  635. .irq_preinstall = msm_irq_preinstall,
  636. .irq_postinstall = msm_irq_postinstall,
  637. .irq_uninstall = msm_irq_uninstall,
  638. .get_vblank_counter = drm_vblank_no_hw_counter,
  639. .enable_vblank = msm_enable_vblank,
  640. .disable_vblank = msm_disable_vblank,
  641. .gem_free_object = msm_gem_free_object,
  642. .gem_vm_ops = &vm_ops,
  643. .dumb_create = msm_gem_dumb_create,
  644. .dumb_map_offset = msm_gem_dumb_map_offset,
  645. .dumb_destroy = drm_gem_dumb_destroy,
  646. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  647. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  648. .gem_prime_export = drm_gem_prime_export,
  649. .gem_prime_import = drm_gem_prime_import,
  650. .gem_prime_pin = msm_gem_prime_pin,
  651. .gem_prime_unpin = msm_gem_prime_unpin,
  652. .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
  653. .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
  654. .gem_prime_vmap = msm_gem_prime_vmap,
  655. .gem_prime_vunmap = msm_gem_prime_vunmap,
  656. .gem_prime_mmap = msm_gem_prime_mmap,
  657. #ifdef CONFIG_DEBUG_FS
  658. .debugfs_init = msm_debugfs_init,
  659. .debugfs_cleanup = msm_debugfs_cleanup,
  660. #endif
  661. .ioctls = msm_ioctls,
  662. .num_ioctls = DRM_MSM_NUM_IOCTLS,
  663. .fops = &fops,
  664. .name = "msm",
  665. .desc = "MSM Snapdragon DRM",
  666. .date = "20130625",
  667. .major = MSM_VERSION_MAJOR,
  668. .minor = MSM_VERSION_MINOR,
  669. .patchlevel = MSM_VERSION_PATCHLEVEL,
  670. };
  671. #ifdef CONFIG_PM_SLEEP
  672. static int msm_pm_suspend(struct device *dev)
  673. {
  674. struct drm_device *ddev = dev_get_drvdata(dev);
  675. drm_kms_helper_poll_disable(ddev);
  676. return 0;
  677. }
  678. static int msm_pm_resume(struct device *dev)
  679. {
  680. struct drm_device *ddev = dev_get_drvdata(dev);
  681. drm_kms_helper_poll_enable(ddev);
  682. return 0;
  683. }
  684. #endif
  685. static const struct dev_pm_ops msm_pm_ops = {
  686. SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
  687. };
  688. /*
  689. * Componentized driver support:
  690. */
  691. /*
  692. * NOTE: duplication of the same code as exynos or imx (or probably any other).
  693. * so probably some room for some helpers
  694. */
  695. static int compare_of(struct device *dev, void *data)
  696. {
  697. return dev->of_node == data;
  698. }
  699. /*
  700. * Identify what components need to be added by parsing what remote-endpoints
  701. * our MDP output ports are connected to. In the case of LVDS on MDP4, there
  702. * is no external component that we need to add since LVDS is within MDP4
  703. * itself.
  704. */
  705. static int add_components_mdp(struct device *mdp_dev,
  706. struct component_match **matchptr)
  707. {
  708. struct device_node *np = mdp_dev->of_node;
  709. struct device_node *ep_node;
  710. struct device *master_dev;
  711. /*
  712. * on MDP4 based platforms, the MDP platform device is the component
  713. * master that adds other display interface components to itself.
  714. *
  715. * on MDP5 based platforms, the MDSS platform device is the component
  716. * master that adds MDP5 and other display interface components to
  717. * itself.
  718. */
  719. if (of_device_is_compatible(np, "qcom,mdp4"))
  720. master_dev = mdp_dev;
  721. else
  722. master_dev = mdp_dev->parent;
  723. for_each_endpoint_of_node(np, ep_node) {
  724. struct device_node *intf;
  725. struct of_endpoint ep;
  726. int ret;
  727. ret = of_graph_parse_endpoint(ep_node, &ep);
  728. if (ret) {
  729. dev_err(mdp_dev, "unable to parse port endpoint\n");
  730. of_node_put(ep_node);
  731. return ret;
  732. }
  733. /*
  734. * The LCDC/LVDS port on MDP4 is a speacial case where the
  735. * remote-endpoint isn't a component that we need to add
  736. */
  737. if (of_device_is_compatible(np, "qcom,mdp4") &&
  738. ep.port == 0) {
  739. of_node_put(ep_node);
  740. continue;
  741. }
  742. /*
  743. * It's okay if some of the ports don't have a remote endpoint
  744. * specified. It just means that the port isn't connected to
  745. * any external interface.
  746. */
  747. intf = of_graph_get_remote_port_parent(ep_node);
  748. if (!intf) {
  749. of_node_put(ep_node);
  750. continue;
  751. }
  752. component_match_add(master_dev, matchptr, compare_of, intf);
  753. of_node_put(intf);
  754. of_node_put(ep_node);
  755. }
  756. return 0;
  757. }
  758. static int compare_name_mdp(struct device *dev, void *data)
  759. {
  760. return (strstr(dev_name(dev), "mdp") != NULL);
  761. }
  762. static int add_display_components(struct device *dev,
  763. struct component_match **matchptr)
  764. {
  765. struct device *mdp_dev;
  766. int ret;
  767. /*
  768. * MDP5 based devices don't have a flat hierarchy. There is a top level
  769. * parent: MDSS, and children: MDP5, DSI, HDMI, eDP etc. Populate the
  770. * children devices, find the MDP5 node, and then add the interfaces
  771. * to our components list.
  772. */
  773. if (of_device_is_compatible(dev->of_node, "qcom,mdss")) {
  774. ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
  775. if (ret) {
  776. dev_err(dev, "failed to populate children devices\n");
  777. return ret;
  778. }
  779. mdp_dev = device_find_child(dev, NULL, compare_name_mdp);
  780. if (!mdp_dev) {
  781. dev_err(dev, "failed to find MDSS MDP node\n");
  782. of_platform_depopulate(dev);
  783. return -ENODEV;
  784. }
  785. put_device(mdp_dev);
  786. /* add the MDP component itself */
  787. component_match_add(dev, matchptr, compare_of,
  788. mdp_dev->of_node);
  789. } else {
  790. /* MDP4 */
  791. mdp_dev = dev;
  792. }
  793. ret = add_components_mdp(mdp_dev, matchptr);
  794. if (ret)
  795. of_platform_depopulate(dev);
  796. return ret;
  797. }
  798. /*
  799. * We don't know what's the best binding to link the gpu with the drm device.
  800. * Fow now, we just hunt for all the possible gpus that we support, and add them
  801. * as components.
  802. */
  803. static const struct of_device_id msm_gpu_match[] = {
  804. { .compatible = "qcom,adreno-3xx" },
  805. { .compatible = "qcom,kgsl-3d0" },
  806. { },
  807. };
  808. static int add_gpu_components(struct device *dev,
  809. struct component_match **matchptr)
  810. {
  811. struct device_node *np;
  812. np = of_find_matching_node(NULL, msm_gpu_match);
  813. if (!np)
  814. return 0;
  815. component_match_add(dev, matchptr, compare_of, np);
  816. of_node_put(np);
  817. return 0;
  818. }
  819. static int msm_drm_bind(struct device *dev)
  820. {
  821. return msm_drm_init(dev, &msm_driver);
  822. }
  823. static void msm_drm_unbind(struct device *dev)
  824. {
  825. msm_drm_uninit(dev);
  826. }
  827. static const struct component_master_ops msm_drm_ops = {
  828. .bind = msm_drm_bind,
  829. .unbind = msm_drm_unbind,
  830. };
  831. /*
  832. * Platform driver:
  833. */
  834. static int msm_pdev_probe(struct platform_device *pdev)
  835. {
  836. struct component_match *match = NULL;
  837. int ret;
  838. ret = add_display_components(&pdev->dev, &match);
  839. if (ret)
  840. return ret;
  841. ret = add_gpu_components(&pdev->dev, &match);
  842. if (ret)
  843. return ret;
  844. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  845. return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
  846. }
  847. static int msm_pdev_remove(struct platform_device *pdev)
  848. {
  849. component_master_del(&pdev->dev, &msm_drm_ops);
  850. of_platform_depopulate(&pdev->dev);
  851. return 0;
  852. }
  853. static const struct of_device_id dt_match[] = {
  854. { .compatible = "qcom,mdp4", .data = (void *)4 }, /* MDP4 */
  855. { .compatible = "qcom,mdss", .data = (void *)5 }, /* MDP5 MDSS */
  856. {}
  857. };
  858. MODULE_DEVICE_TABLE(of, dt_match);
  859. static struct platform_driver msm_platform_driver = {
  860. .probe = msm_pdev_probe,
  861. .remove = msm_pdev_remove,
  862. .driver = {
  863. .name = "msm",
  864. .of_match_table = dt_match,
  865. .pm = &msm_pm_ops,
  866. },
  867. };
  868. static int __init msm_drm_register(void)
  869. {
  870. DBG("init");
  871. msm_mdp_register();
  872. msm_dsi_register();
  873. msm_edp_register();
  874. msm_hdmi_register();
  875. adreno_register();
  876. return platform_driver_register(&msm_platform_driver);
  877. }
  878. static void __exit msm_drm_unregister(void)
  879. {
  880. DBG("fini");
  881. platform_driver_unregister(&msm_platform_driver);
  882. msm_hdmi_unregister();
  883. adreno_unregister();
  884. msm_edp_unregister();
  885. msm_dsi_unregister();
  886. msm_mdp_unregister();
  887. }
  888. module_init(msm_drm_register);
  889. module_exit(msm_drm_unregister);
  890. MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
  891. MODULE_DESCRIPTION("MSM DRM Driver");
  892. MODULE_LICENSE("GPL");