msm_drv.c 27 KB

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