msm_drv.c 27 KB

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