msm_drv.c 26 KB

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