msm_drv.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "msm_drv.h"
  18. #include "msm_gpu.h"
  19. #include "msm_kms.h"
  20. static void msm_fb_output_poll_changed(struct drm_device *dev)
  21. {
  22. struct msm_drm_private *priv = dev->dev_private;
  23. if (priv->fbdev)
  24. drm_fb_helper_hotplug_event(priv->fbdev);
  25. }
  26. static const struct drm_mode_config_funcs mode_config_funcs = {
  27. .fb_create = msm_framebuffer_create,
  28. .output_poll_changed = msm_fb_output_poll_changed,
  29. .atomic_check = msm_atomic_check,
  30. .atomic_commit = msm_atomic_commit,
  31. };
  32. int msm_register_mmu(struct drm_device *dev, struct msm_mmu *mmu)
  33. {
  34. struct msm_drm_private *priv = dev->dev_private;
  35. int idx = priv->num_mmus++;
  36. if (WARN_ON(idx >= ARRAY_SIZE(priv->mmus)))
  37. return -EINVAL;
  38. priv->mmus[idx] = mmu;
  39. return idx;
  40. }
  41. #ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
  42. static bool reglog = false;
  43. MODULE_PARM_DESC(reglog, "Enable register read/write logging");
  44. module_param(reglog, bool, 0600);
  45. #else
  46. #define reglog 0
  47. #endif
  48. #ifdef CONFIG_DRM_FBDEV_EMULATION
  49. static bool fbdev = true;
  50. MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
  51. module_param(fbdev, bool, 0600);
  52. #endif
  53. static char *vram = "16m";
  54. MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU");
  55. module_param(vram, charp, 0);
  56. /*
  57. * Util/helpers:
  58. */
  59. void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
  60. const char *dbgname)
  61. {
  62. struct resource *res;
  63. unsigned long size;
  64. void __iomem *ptr;
  65. if (name)
  66. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  67. else
  68. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  69. if (!res) {
  70. dev_err(&pdev->dev, "failed to get memory resource: %s\n", name);
  71. return ERR_PTR(-EINVAL);
  72. }
  73. size = resource_size(res);
  74. ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
  75. if (!ptr) {
  76. dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
  77. return ERR_PTR(-ENOMEM);
  78. }
  79. if (reglog)
  80. printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
  81. return ptr;
  82. }
  83. void msm_writel(u32 data, void __iomem *addr)
  84. {
  85. if (reglog)
  86. printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
  87. writel(data, addr);
  88. }
  89. u32 msm_readl(const void __iomem *addr)
  90. {
  91. u32 val = readl(addr);
  92. if (reglog)
  93. printk(KERN_ERR "IO:R %p %08x\n", addr, val);
  94. return val;
  95. }
  96. struct vblank_event {
  97. struct list_head node;
  98. int crtc_id;
  99. bool enable;
  100. };
  101. static void vblank_ctrl_worker(struct work_struct *work)
  102. {
  103. struct msm_vblank_ctrl *vbl_ctrl = container_of(work,
  104. struct msm_vblank_ctrl, work);
  105. struct msm_drm_private *priv = container_of(vbl_ctrl,
  106. struct msm_drm_private, vblank_ctrl);
  107. struct msm_kms *kms = priv->kms;
  108. struct vblank_event *vbl_ev, *tmp;
  109. unsigned long flags;
  110. spin_lock_irqsave(&vbl_ctrl->lock, flags);
  111. list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
  112. list_del(&vbl_ev->node);
  113. spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
  114. if (vbl_ev->enable)
  115. kms->funcs->enable_vblank(kms,
  116. priv->crtcs[vbl_ev->crtc_id]);
  117. else
  118. kms->funcs->disable_vblank(kms,
  119. priv->crtcs[vbl_ev->crtc_id]);
  120. kfree(vbl_ev);
  121. spin_lock_irqsave(&vbl_ctrl->lock, flags);
  122. }
  123. spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
  124. }
  125. static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
  126. int crtc_id, bool enable)
  127. {
  128. struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
  129. struct vblank_event *vbl_ev;
  130. unsigned long flags;
  131. vbl_ev = kzalloc(sizeof(*vbl_ev), GFP_ATOMIC);
  132. if (!vbl_ev)
  133. return -ENOMEM;
  134. vbl_ev->crtc_id = crtc_id;
  135. vbl_ev->enable = enable;
  136. spin_lock_irqsave(&vbl_ctrl->lock, flags);
  137. list_add_tail(&vbl_ev->node, &vbl_ctrl->event_list);
  138. spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
  139. queue_work(priv->wq, &vbl_ctrl->work);
  140. return 0;
  141. }
  142. /*
  143. * DRM operations:
  144. */
  145. static int msm_unload(struct drm_device *dev)
  146. {
  147. struct msm_drm_private *priv = dev->dev_private;
  148. struct msm_kms *kms = priv->kms;
  149. struct msm_gpu *gpu = priv->gpu;
  150. struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
  151. struct vblank_event *vbl_ev, *tmp;
  152. /* We must cancel and cleanup any pending vblank enable/disable
  153. * work before drm_irq_uninstall() to avoid work re-enabling an
  154. * irq after uninstall has disabled it.
  155. */
  156. cancel_work_sync(&vbl_ctrl->work);
  157. list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
  158. list_del(&vbl_ev->node);
  159. kfree(vbl_ev);
  160. }
  161. drm_kms_helper_poll_fini(dev);
  162. drm_mode_config_cleanup(dev);
  163. drm_vblank_cleanup(dev);
  164. pm_runtime_get_sync(dev->dev);
  165. drm_irq_uninstall(dev);
  166. pm_runtime_put_sync(dev->dev);
  167. flush_workqueue(priv->wq);
  168. destroy_workqueue(priv->wq);
  169. if (kms) {
  170. pm_runtime_disable(dev->dev);
  171. kms->funcs->destroy(kms);
  172. }
  173. if (gpu) {
  174. mutex_lock(&dev->struct_mutex);
  175. gpu->funcs->pm_suspend(gpu);
  176. mutex_unlock(&dev->struct_mutex);
  177. gpu->funcs->destroy(gpu);
  178. }
  179. if (priv->vram.paddr) {
  180. DEFINE_DMA_ATTRS(attrs);
  181. dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
  182. drm_mm_takedown(&priv->vram.mm);
  183. dma_free_attrs(dev->dev, priv->vram.size, NULL,
  184. priv->vram.paddr, &attrs);
  185. }
  186. component_unbind_all(dev->dev, dev);
  187. dev->dev_private = NULL;
  188. kfree(priv);
  189. return 0;
  190. }
  191. static int get_mdp_ver(struct platform_device *pdev)
  192. {
  193. #ifdef CONFIG_OF
  194. static const struct of_device_id match_types[] = { {
  195. .compatible = "qcom,mdss_mdp",
  196. .data = (void *)5,
  197. }, {
  198. /* end node */
  199. } };
  200. struct device *dev = &pdev->dev;
  201. const struct of_device_id *match;
  202. match = of_match_node(match_types, dev->of_node);
  203. if (match)
  204. return (int)(unsigned long)match->data;
  205. #endif
  206. return 4;
  207. }
  208. #include <linux/of_address.h>
  209. static int msm_init_vram(struct drm_device *dev)
  210. {
  211. struct msm_drm_private *priv = dev->dev_private;
  212. unsigned long size = 0;
  213. int ret = 0;
  214. #ifdef CONFIG_OF
  215. /* In the device-tree world, we could have a 'memory-region'
  216. * phandle, which gives us a link to our "vram". Allocating
  217. * is all nicely abstracted behind the dma api, but we need
  218. * to know the entire size to allocate it all in one go. There
  219. * are two cases:
  220. * 1) device with no IOMMU, in which case we need exclusive
  221. * access to a VRAM carveout big enough for all gpu
  222. * buffers
  223. * 2) device with IOMMU, but where the bootloader puts up
  224. * a splash screen. In this case, the VRAM carveout
  225. * need only be large enough for fbdev fb. But we need
  226. * exclusive access to the buffer to avoid the kernel
  227. * using those pages for other purposes (which appears
  228. * as corruption on screen before we have a chance to
  229. * load and do initial modeset)
  230. */
  231. struct device_node *node;
  232. node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
  233. if (node) {
  234. struct resource r;
  235. ret = of_address_to_resource(node, 0, &r);
  236. if (ret)
  237. return ret;
  238. size = r.end - r.start;
  239. DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
  240. } else
  241. #endif
  242. /* if we have no IOMMU, then we need to use carveout allocator.
  243. * Grab the entire CMA chunk carved out in early startup in
  244. * mach-msm:
  245. */
  246. if (!iommu_present(&platform_bus_type)) {
  247. DRM_INFO("using %s VRAM carveout\n", vram);
  248. size = memparse(vram, NULL);
  249. }
  250. if (size) {
  251. DEFINE_DMA_ATTRS(attrs);
  252. void *p;
  253. priv->vram.size = size;
  254. drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
  255. dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
  256. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  257. /* note that for no-kernel-mapping, the vaddr returned
  258. * is bogus, but non-null if allocation succeeded:
  259. */
  260. p = dma_alloc_attrs(dev->dev, size,
  261. &priv->vram.paddr, GFP_KERNEL, &attrs);
  262. if (!p) {
  263. dev_err(dev->dev, "failed to allocate VRAM\n");
  264. priv->vram.paddr = 0;
  265. return -ENOMEM;
  266. }
  267. dev_info(dev->dev, "VRAM: %08x->%08x\n",
  268. (uint32_t)priv->vram.paddr,
  269. (uint32_t)(priv->vram.paddr + size));
  270. }
  271. return ret;
  272. }
  273. static int msm_load(struct drm_device *dev, unsigned long flags)
  274. {
  275. struct platform_device *pdev = dev->platformdev;
  276. struct msm_drm_private *priv;
  277. struct msm_kms *kms;
  278. int ret;
  279. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  280. if (!priv) {
  281. dev_err(dev->dev, "failed to allocate private data\n");
  282. return -ENOMEM;
  283. }
  284. dev->dev_private = priv;
  285. priv->wq = alloc_ordered_workqueue("msm", 0);
  286. init_waitqueue_head(&priv->fence_event);
  287. init_waitqueue_head(&priv->pending_crtcs_event);
  288. INIT_LIST_HEAD(&priv->inactive_list);
  289. INIT_LIST_HEAD(&priv->fence_cbs);
  290. INIT_LIST_HEAD(&priv->vblank_ctrl.event_list);
  291. INIT_WORK(&priv->vblank_ctrl.work, vblank_ctrl_worker);
  292. spin_lock_init(&priv->vblank_ctrl.lock);
  293. drm_mode_config_init(dev);
  294. platform_set_drvdata(pdev, dev);
  295. /* Bind all our sub-components: */
  296. ret = component_bind_all(dev->dev, dev);
  297. if (ret)
  298. return ret;
  299. ret = msm_init_vram(dev);
  300. if (ret)
  301. goto fail;
  302. switch (get_mdp_ver(pdev)) {
  303. case 4:
  304. kms = mdp4_kms_init(dev);
  305. break;
  306. case 5:
  307. kms = mdp5_kms_init(dev);
  308. break;
  309. default:
  310. kms = ERR_PTR(-ENODEV);
  311. break;
  312. }
  313. if (IS_ERR(kms)) {
  314. /*
  315. * NOTE: once we have GPU support, having no kms should not
  316. * be considered fatal.. ideally we would still support gpu
  317. * and (for example) use dmabuf/prime to share buffers with
  318. * imx drm driver on iMX5
  319. */
  320. dev_err(dev->dev, "failed to load kms\n");
  321. ret = PTR_ERR(kms);
  322. goto fail;
  323. }
  324. priv->kms = kms;
  325. if (kms) {
  326. pm_runtime_enable(dev->dev);
  327. ret = kms->funcs->hw_init(kms);
  328. if (ret) {
  329. dev_err(dev->dev, "kms hw init failed: %d\n", ret);
  330. goto fail;
  331. }
  332. }
  333. dev->mode_config.funcs = &mode_config_funcs;
  334. ret = drm_vblank_init(dev, priv->num_crtcs);
  335. if (ret < 0) {
  336. dev_err(dev->dev, "failed to initialize vblank\n");
  337. goto fail;
  338. }
  339. pm_runtime_get_sync(dev->dev);
  340. ret = drm_irq_install(dev, platform_get_irq(dev->platformdev, 0));
  341. pm_runtime_put_sync(dev->dev);
  342. if (ret < 0) {
  343. dev_err(dev->dev, "failed to install IRQ handler\n");
  344. goto fail;
  345. }
  346. drm_mode_config_reset(dev);
  347. #ifdef CONFIG_DRM_FBDEV_EMULATION
  348. if (fbdev)
  349. priv->fbdev = msm_fbdev_init(dev);
  350. #endif
  351. ret = msm_debugfs_late_init(dev);
  352. if (ret)
  353. goto fail;
  354. drm_kms_helper_poll_init(dev);
  355. return 0;
  356. fail:
  357. msm_unload(dev);
  358. return ret;
  359. }
  360. static void load_gpu(struct drm_device *dev)
  361. {
  362. static DEFINE_MUTEX(init_lock);
  363. struct msm_drm_private *priv = dev->dev_private;
  364. mutex_lock(&init_lock);
  365. if (!priv->gpu)
  366. priv->gpu = adreno_load_gpu(dev);
  367. mutex_unlock(&init_lock);
  368. }
  369. static int msm_open(struct drm_device *dev, struct drm_file *file)
  370. {
  371. struct msm_file_private *ctx;
  372. /* For now, load gpu on open.. to avoid the requirement of having
  373. * firmware in the initrd.
  374. */
  375. load_gpu(dev);
  376. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  377. if (!ctx)
  378. return -ENOMEM;
  379. file->driver_priv = ctx;
  380. return 0;
  381. }
  382. static void msm_preclose(struct drm_device *dev, struct drm_file *file)
  383. {
  384. struct msm_drm_private *priv = dev->dev_private;
  385. struct msm_file_private *ctx = file->driver_priv;
  386. struct msm_kms *kms = priv->kms;
  387. if (kms)
  388. kms->funcs->preclose(kms, file);
  389. mutex_lock(&dev->struct_mutex);
  390. if (ctx == priv->lastctx)
  391. priv->lastctx = NULL;
  392. mutex_unlock(&dev->struct_mutex);
  393. kfree(ctx);
  394. }
  395. static void msm_lastclose(struct drm_device *dev)
  396. {
  397. struct msm_drm_private *priv = dev->dev_private;
  398. if (priv->fbdev)
  399. drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
  400. }
  401. static irqreturn_t msm_irq(int irq, void *arg)
  402. {
  403. struct drm_device *dev = arg;
  404. struct msm_drm_private *priv = dev->dev_private;
  405. struct msm_kms *kms = priv->kms;
  406. BUG_ON(!kms);
  407. return kms->funcs->irq(kms);
  408. }
  409. static void msm_irq_preinstall(struct drm_device *dev)
  410. {
  411. struct msm_drm_private *priv = dev->dev_private;
  412. struct msm_kms *kms = priv->kms;
  413. BUG_ON(!kms);
  414. kms->funcs->irq_preinstall(kms);
  415. }
  416. static int msm_irq_postinstall(struct drm_device *dev)
  417. {
  418. struct msm_drm_private *priv = dev->dev_private;
  419. struct msm_kms *kms = priv->kms;
  420. BUG_ON(!kms);
  421. return kms->funcs->irq_postinstall(kms);
  422. }
  423. static void msm_irq_uninstall(struct drm_device *dev)
  424. {
  425. struct msm_drm_private *priv = dev->dev_private;
  426. struct msm_kms *kms = priv->kms;
  427. BUG_ON(!kms);
  428. kms->funcs->irq_uninstall(kms);
  429. }
  430. static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe)
  431. {
  432. struct msm_drm_private *priv = dev->dev_private;
  433. struct msm_kms *kms = priv->kms;
  434. if (!kms)
  435. return -ENXIO;
  436. DBG("dev=%p, crtc=%u", dev, pipe);
  437. return vblank_ctrl_queue_work(priv, pipe, true);
  438. }
  439. static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe)
  440. {
  441. struct msm_drm_private *priv = dev->dev_private;
  442. struct msm_kms *kms = priv->kms;
  443. if (!kms)
  444. return;
  445. DBG("dev=%p, crtc=%u", dev, pipe);
  446. vblank_ctrl_queue_work(priv, pipe, false);
  447. }
  448. /*
  449. * DRM debugfs:
  450. */
  451. #ifdef CONFIG_DEBUG_FS
  452. static int msm_gpu_show(struct drm_device *dev, struct seq_file *m)
  453. {
  454. struct msm_drm_private *priv = dev->dev_private;
  455. struct msm_gpu *gpu = priv->gpu;
  456. if (gpu) {
  457. seq_printf(m, "%s Status:\n", gpu->name);
  458. gpu->funcs->show(gpu, m);
  459. }
  460. return 0;
  461. }
  462. static int msm_gem_show(struct drm_device *dev, struct seq_file *m)
  463. {
  464. struct msm_drm_private *priv = dev->dev_private;
  465. struct msm_gpu *gpu = priv->gpu;
  466. if (gpu) {
  467. seq_printf(m, "Active Objects (%s):\n", gpu->name);
  468. msm_gem_describe_objects(&gpu->active_list, m);
  469. }
  470. seq_printf(m, "Inactive Objects:\n");
  471. msm_gem_describe_objects(&priv->inactive_list, m);
  472. return 0;
  473. }
  474. static int msm_mm_show(struct drm_device *dev, struct seq_file *m)
  475. {
  476. return drm_mm_dump_table(m, &dev->vma_offset_manager->vm_addr_space_mm);
  477. }
  478. static int msm_fb_show(struct drm_device *dev, struct seq_file *m)
  479. {
  480. struct msm_drm_private *priv = dev->dev_private;
  481. struct drm_framebuffer *fb, *fbdev_fb = NULL;
  482. if (priv->fbdev) {
  483. seq_printf(m, "fbcon ");
  484. fbdev_fb = priv->fbdev->fb;
  485. msm_framebuffer_describe(fbdev_fb, m);
  486. }
  487. mutex_lock(&dev->mode_config.fb_lock);
  488. list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
  489. if (fb == fbdev_fb)
  490. continue;
  491. seq_printf(m, "user ");
  492. msm_framebuffer_describe(fb, m);
  493. }
  494. mutex_unlock(&dev->mode_config.fb_lock);
  495. return 0;
  496. }
  497. static int show_locked(struct seq_file *m, void *arg)
  498. {
  499. struct drm_info_node *node = (struct drm_info_node *) m->private;
  500. struct drm_device *dev = node->minor->dev;
  501. int (*show)(struct drm_device *dev, struct seq_file *m) =
  502. node->info_ent->data;
  503. int ret;
  504. ret = mutex_lock_interruptible(&dev->struct_mutex);
  505. if (ret)
  506. return ret;
  507. ret = show(dev, m);
  508. mutex_unlock(&dev->struct_mutex);
  509. return ret;
  510. }
  511. static struct drm_info_list msm_debugfs_list[] = {
  512. {"gpu", show_locked, 0, msm_gpu_show},
  513. {"gem", show_locked, 0, msm_gem_show},
  514. { "mm", show_locked, 0, msm_mm_show },
  515. { "fb", show_locked, 0, msm_fb_show },
  516. };
  517. static int late_init_minor(struct drm_minor *minor)
  518. {
  519. int ret;
  520. if (!minor)
  521. return 0;
  522. ret = msm_rd_debugfs_init(minor);
  523. if (ret) {
  524. dev_err(minor->dev->dev, "could not install rd debugfs\n");
  525. return ret;
  526. }
  527. ret = msm_perf_debugfs_init(minor);
  528. if (ret) {
  529. dev_err(minor->dev->dev, "could not install perf debugfs\n");
  530. return ret;
  531. }
  532. return 0;
  533. }
  534. int msm_debugfs_late_init(struct drm_device *dev)
  535. {
  536. int ret;
  537. ret = late_init_minor(dev->primary);
  538. if (ret)
  539. return ret;
  540. ret = late_init_minor(dev->render);
  541. if (ret)
  542. return ret;
  543. ret = late_init_minor(dev->control);
  544. return ret;
  545. }
  546. static int msm_debugfs_init(struct drm_minor *minor)
  547. {
  548. struct drm_device *dev = minor->dev;
  549. int ret;
  550. ret = drm_debugfs_create_files(msm_debugfs_list,
  551. ARRAY_SIZE(msm_debugfs_list),
  552. minor->debugfs_root, minor);
  553. if (ret) {
  554. dev_err(dev->dev, "could not install msm_debugfs_list\n");
  555. return ret;
  556. }
  557. return 0;
  558. }
  559. static void msm_debugfs_cleanup(struct drm_minor *minor)
  560. {
  561. drm_debugfs_remove_files(msm_debugfs_list,
  562. ARRAY_SIZE(msm_debugfs_list), minor);
  563. if (!minor->dev->dev_private)
  564. return;
  565. msm_rd_debugfs_cleanup(minor);
  566. msm_perf_debugfs_cleanup(minor);
  567. }
  568. #endif
  569. /*
  570. * Fences:
  571. */
  572. int msm_wait_fence(struct drm_device *dev, uint32_t fence,
  573. ktime_t *timeout , bool interruptible)
  574. {
  575. struct msm_drm_private *priv = dev->dev_private;
  576. int ret;
  577. if (!priv->gpu)
  578. return 0;
  579. if (fence > priv->gpu->submitted_fence) {
  580. DRM_ERROR("waiting on invalid fence: %u (of %u)\n",
  581. fence, priv->gpu->submitted_fence);
  582. return -EINVAL;
  583. }
  584. if (!timeout) {
  585. /* no-wait: */
  586. ret = fence_completed(dev, fence) ? 0 : -EBUSY;
  587. } else {
  588. ktime_t now = ktime_get();
  589. unsigned long remaining_jiffies;
  590. if (ktime_compare(*timeout, now) < 0) {
  591. remaining_jiffies = 0;
  592. } else {
  593. ktime_t rem = ktime_sub(*timeout, now);
  594. struct timespec ts = ktime_to_timespec(rem);
  595. remaining_jiffies = timespec_to_jiffies(&ts);
  596. }
  597. if (interruptible)
  598. ret = wait_event_interruptible_timeout(priv->fence_event,
  599. fence_completed(dev, fence),
  600. remaining_jiffies);
  601. else
  602. ret = wait_event_timeout(priv->fence_event,
  603. fence_completed(dev, fence),
  604. remaining_jiffies);
  605. if (ret == 0) {
  606. DBG("timeout waiting for fence: %u (completed: %u)",
  607. fence, priv->completed_fence);
  608. ret = -ETIMEDOUT;
  609. } else if (ret != -ERESTARTSYS) {
  610. ret = 0;
  611. }
  612. }
  613. return ret;
  614. }
  615. int msm_queue_fence_cb(struct drm_device *dev,
  616. struct msm_fence_cb *cb, uint32_t fence)
  617. {
  618. struct msm_drm_private *priv = dev->dev_private;
  619. int ret = 0;
  620. mutex_lock(&dev->struct_mutex);
  621. if (!list_empty(&cb->work.entry)) {
  622. ret = -EINVAL;
  623. } else if (fence > priv->completed_fence) {
  624. cb->fence = fence;
  625. list_add_tail(&cb->work.entry, &priv->fence_cbs);
  626. } else {
  627. queue_work(priv->wq, &cb->work);
  628. }
  629. mutex_unlock(&dev->struct_mutex);
  630. return ret;
  631. }
  632. /* called from workqueue */
  633. void msm_update_fence(struct drm_device *dev, uint32_t fence)
  634. {
  635. struct msm_drm_private *priv = dev->dev_private;
  636. mutex_lock(&dev->struct_mutex);
  637. priv->completed_fence = max(fence, priv->completed_fence);
  638. while (!list_empty(&priv->fence_cbs)) {
  639. struct msm_fence_cb *cb;
  640. cb = list_first_entry(&priv->fence_cbs,
  641. struct msm_fence_cb, work.entry);
  642. if (cb->fence > priv->completed_fence)
  643. break;
  644. list_del_init(&cb->work.entry);
  645. queue_work(priv->wq, &cb->work);
  646. }
  647. mutex_unlock(&dev->struct_mutex);
  648. wake_up_all(&priv->fence_event);
  649. }
  650. void __msm_fence_worker(struct work_struct *work)
  651. {
  652. struct msm_fence_cb *cb = container_of(work, struct msm_fence_cb, work);
  653. cb->func(cb);
  654. }
  655. /*
  656. * DRM ioctls:
  657. */
  658. static int msm_ioctl_get_param(struct drm_device *dev, void *data,
  659. struct drm_file *file)
  660. {
  661. struct msm_drm_private *priv = dev->dev_private;
  662. struct drm_msm_param *args = data;
  663. struct msm_gpu *gpu;
  664. /* for now, we just have 3d pipe.. eventually this would need to
  665. * be more clever to dispatch to appropriate gpu module:
  666. */
  667. if (args->pipe != MSM_PIPE_3D0)
  668. return -EINVAL;
  669. gpu = priv->gpu;
  670. if (!gpu)
  671. return -ENXIO;
  672. return gpu->funcs->get_param(gpu, args->param, &args->value);
  673. }
  674. static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
  675. struct drm_file *file)
  676. {
  677. struct drm_msm_gem_new *args = data;
  678. if (args->flags & ~MSM_BO_FLAGS) {
  679. DRM_ERROR("invalid flags: %08x\n", args->flags);
  680. return -EINVAL;
  681. }
  682. return msm_gem_new_handle(dev, file, args->size,
  683. args->flags, &args->handle);
  684. }
  685. static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
  686. {
  687. return ktime_set(timeout.tv_sec, timeout.tv_nsec);
  688. }
  689. static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
  690. struct drm_file *file)
  691. {
  692. struct drm_msm_gem_cpu_prep *args = data;
  693. struct drm_gem_object *obj;
  694. ktime_t timeout = to_ktime(args->timeout);
  695. int ret;
  696. if (args->op & ~MSM_PREP_FLAGS) {
  697. DRM_ERROR("invalid op: %08x\n", args->op);
  698. return -EINVAL;
  699. }
  700. obj = drm_gem_object_lookup(dev, file, args->handle);
  701. if (!obj)
  702. return -ENOENT;
  703. ret = msm_gem_cpu_prep(obj, args->op, &timeout);
  704. drm_gem_object_unreference_unlocked(obj);
  705. return ret;
  706. }
  707. static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
  708. struct drm_file *file)
  709. {
  710. struct drm_msm_gem_cpu_fini *args = data;
  711. struct drm_gem_object *obj;
  712. int ret;
  713. obj = drm_gem_object_lookup(dev, file, args->handle);
  714. if (!obj)
  715. return -ENOENT;
  716. ret = msm_gem_cpu_fini(obj);
  717. drm_gem_object_unreference_unlocked(obj);
  718. return ret;
  719. }
  720. static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
  721. struct drm_file *file)
  722. {
  723. struct drm_msm_gem_info *args = data;
  724. struct drm_gem_object *obj;
  725. int ret = 0;
  726. if (args->pad)
  727. return -EINVAL;
  728. obj = drm_gem_object_lookup(dev, file, args->handle);
  729. if (!obj)
  730. return -ENOENT;
  731. args->offset = msm_gem_mmap_offset(obj);
  732. drm_gem_object_unreference_unlocked(obj);
  733. return ret;
  734. }
  735. static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
  736. struct drm_file *file)
  737. {
  738. struct drm_msm_wait_fence *args = data;
  739. ktime_t timeout = to_ktime(args->timeout);
  740. if (args->pad) {
  741. DRM_ERROR("invalid pad: %08x\n", args->pad);
  742. return -EINVAL;
  743. }
  744. return msm_wait_fence(dev, args->fence, &timeout, true);
  745. }
  746. static const struct drm_ioctl_desc msm_ioctls[] = {
  747. DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_AUTH|DRM_RENDER_ALLOW),
  748. DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_AUTH|DRM_RENDER_ALLOW),
  749. DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_AUTH|DRM_RENDER_ALLOW),
  750. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
  751. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
  752. DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_AUTH|DRM_RENDER_ALLOW),
  753. DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_AUTH|DRM_RENDER_ALLOW),
  754. };
  755. static const struct vm_operations_struct vm_ops = {
  756. .fault = msm_gem_fault,
  757. .open = drm_gem_vm_open,
  758. .close = drm_gem_vm_close,
  759. };
  760. static const struct file_operations fops = {
  761. .owner = THIS_MODULE,
  762. .open = drm_open,
  763. .release = drm_release,
  764. .unlocked_ioctl = drm_ioctl,
  765. #ifdef CONFIG_COMPAT
  766. .compat_ioctl = drm_compat_ioctl,
  767. #endif
  768. .poll = drm_poll,
  769. .read = drm_read,
  770. .llseek = no_llseek,
  771. .mmap = msm_gem_mmap,
  772. };
  773. static struct drm_driver msm_driver = {
  774. .driver_features = DRIVER_HAVE_IRQ |
  775. DRIVER_GEM |
  776. DRIVER_PRIME |
  777. DRIVER_RENDER |
  778. DRIVER_ATOMIC |
  779. DRIVER_MODESET,
  780. .load = msm_load,
  781. .unload = msm_unload,
  782. .open = msm_open,
  783. .preclose = msm_preclose,
  784. .lastclose = msm_lastclose,
  785. .set_busid = drm_platform_set_busid,
  786. .irq_handler = msm_irq,
  787. .irq_preinstall = msm_irq_preinstall,
  788. .irq_postinstall = msm_irq_postinstall,
  789. .irq_uninstall = msm_irq_uninstall,
  790. .get_vblank_counter = drm_vblank_no_hw_counter,
  791. .enable_vblank = msm_enable_vblank,
  792. .disable_vblank = msm_disable_vblank,
  793. .gem_free_object = msm_gem_free_object,
  794. .gem_vm_ops = &vm_ops,
  795. .dumb_create = msm_gem_dumb_create,
  796. .dumb_map_offset = msm_gem_dumb_map_offset,
  797. .dumb_destroy = drm_gem_dumb_destroy,
  798. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  799. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  800. .gem_prime_export = drm_gem_prime_export,
  801. .gem_prime_import = drm_gem_prime_import,
  802. .gem_prime_res_obj = msm_gem_prime_res_obj,
  803. .gem_prime_pin = msm_gem_prime_pin,
  804. .gem_prime_unpin = msm_gem_prime_unpin,
  805. .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
  806. .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
  807. .gem_prime_vmap = msm_gem_prime_vmap,
  808. .gem_prime_vunmap = msm_gem_prime_vunmap,
  809. .gem_prime_mmap = msm_gem_prime_mmap,
  810. #ifdef CONFIG_DEBUG_FS
  811. .debugfs_init = msm_debugfs_init,
  812. .debugfs_cleanup = msm_debugfs_cleanup,
  813. #endif
  814. .ioctls = msm_ioctls,
  815. .num_ioctls = DRM_MSM_NUM_IOCTLS,
  816. .fops = &fops,
  817. .name = "msm",
  818. .desc = "MSM Snapdragon DRM",
  819. .date = "20130625",
  820. .major = 1,
  821. .minor = 0,
  822. };
  823. #ifdef CONFIG_PM_SLEEP
  824. static int msm_pm_suspend(struct device *dev)
  825. {
  826. struct drm_device *ddev = dev_get_drvdata(dev);
  827. drm_kms_helper_poll_disable(ddev);
  828. return 0;
  829. }
  830. static int msm_pm_resume(struct device *dev)
  831. {
  832. struct drm_device *ddev = dev_get_drvdata(dev);
  833. drm_kms_helper_poll_enable(ddev);
  834. return 0;
  835. }
  836. #endif
  837. static const struct dev_pm_ops msm_pm_ops = {
  838. SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
  839. };
  840. /*
  841. * Componentized driver support:
  842. */
  843. #ifdef CONFIG_OF
  844. /* NOTE: the CONFIG_OF case duplicates the same code as exynos or imx
  845. * (or probably any other).. so probably some room for some helpers
  846. */
  847. static int compare_of(struct device *dev, void *data)
  848. {
  849. return dev->of_node == data;
  850. }
  851. static int add_components(struct device *dev, struct component_match **matchptr,
  852. const char *name)
  853. {
  854. struct device_node *np = dev->of_node;
  855. unsigned i;
  856. for (i = 0; ; i++) {
  857. struct device_node *node;
  858. node = of_parse_phandle(np, name, i);
  859. if (!node)
  860. break;
  861. component_match_add(dev, matchptr, compare_of, node);
  862. }
  863. return 0;
  864. }
  865. #else
  866. static int compare_dev(struct device *dev, void *data)
  867. {
  868. return dev == data;
  869. }
  870. #endif
  871. static int msm_drm_bind(struct device *dev)
  872. {
  873. return drm_platform_init(&msm_driver, to_platform_device(dev));
  874. }
  875. static void msm_drm_unbind(struct device *dev)
  876. {
  877. drm_put_dev(platform_get_drvdata(to_platform_device(dev)));
  878. }
  879. static const struct component_master_ops msm_drm_ops = {
  880. .bind = msm_drm_bind,
  881. .unbind = msm_drm_unbind,
  882. };
  883. /*
  884. * Platform driver:
  885. */
  886. static int msm_pdev_probe(struct platform_device *pdev)
  887. {
  888. struct component_match *match = NULL;
  889. #ifdef CONFIG_OF
  890. add_components(&pdev->dev, &match, "connectors");
  891. add_components(&pdev->dev, &match, "gpus");
  892. #else
  893. /* For non-DT case, it kinda sucks. We don't actually have a way
  894. * to know whether or not we are waiting for certain devices (or if
  895. * they are simply not present). But for non-DT we only need to
  896. * care about apq8064/apq8060/etc (all mdp4/a3xx):
  897. */
  898. static const char *devnames[] = {
  899. "hdmi_msm.0", "kgsl-3d0.0",
  900. };
  901. int i;
  902. DBG("Adding components..");
  903. for (i = 0; i < ARRAY_SIZE(devnames); i++) {
  904. struct device *dev;
  905. dev = bus_find_device_by_name(&platform_bus_type,
  906. NULL, devnames[i]);
  907. if (!dev) {
  908. dev_info(&pdev->dev, "still waiting for %s\n", devnames[i]);
  909. return -EPROBE_DEFER;
  910. }
  911. component_match_add(&pdev->dev, &match, compare_dev, dev);
  912. }
  913. #endif
  914. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  915. return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
  916. }
  917. static int msm_pdev_remove(struct platform_device *pdev)
  918. {
  919. component_master_del(&pdev->dev, &msm_drm_ops);
  920. return 0;
  921. }
  922. static const struct platform_device_id msm_id[] = {
  923. { "mdp", 0 },
  924. { }
  925. };
  926. static const struct of_device_id dt_match[] = {
  927. { .compatible = "qcom,mdp" }, /* mdp4 */
  928. { .compatible = "qcom,mdss_mdp" }, /* mdp5 */
  929. {}
  930. };
  931. MODULE_DEVICE_TABLE(of, dt_match);
  932. static struct platform_driver msm_platform_driver = {
  933. .probe = msm_pdev_probe,
  934. .remove = msm_pdev_remove,
  935. .driver = {
  936. .name = "msm",
  937. .of_match_table = dt_match,
  938. .pm = &msm_pm_ops,
  939. },
  940. .id_table = msm_id,
  941. };
  942. static int __init msm_drm_register(void)
  943. {
  944. DBG("init");
  945. msm_dsi_register();
  946. msm_edp_register();
  947. hdmi_register();
  948. adreno_register();
  949. return platform_driver_register(&msm_platform_driver);
  950. }
  951. static void __exit msm_drm_unregister(void)
  952. {
  953. DBG("fini");
  954. platform_driver_unregister(&msm_platform_driver);
  955. hdmi_unregister();
  956. adreno_unregister();
  957. msm_edp_unregister();
  958. msm_dsi_unregister();
  959. }
  960. module_init(msm_drm_register);
  961. module_exit(msm_drm_unregister);
  962. MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
  963. MODULE_DESCRIPTION("MSM DRM Driver");
  964. MODULE_LICENSE("GPL");