msm_drv.c 24 KB

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