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