msm_drv.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 = "16m";
  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, priv->num_crtcs);
  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. static DEFINE_MUTEX(init_lock);
  258. struct msm_drm_private *priv = dev->dev_private;
  259. mutex_lock(&init_lock);
  260. if (!priv->gpu)
  261. priv->gpu = adreno_load_gpu(dev);
  262. mutex_unlock(&init_lock);
  263. }
  264. static int msm_open(struct drm_device *dev, struct drm_file *file)
  265. {
  266. struct msm_file_private *ctx;
  267. /* For now, load gpu on open.. to avoid the requirement of having
  268. * firmware in the initrd.
  269. */
  270. load_gpu(dev);
  271. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  272. if (!ctx)
  273. return -ENOMEM;
  274. file->driver_priv = ctx;
  275. return 0;
  276. }
  277. static void msm_preclose(struct drm_device *dev, struct drm_file *file)
  278. {
  279. struct msm_drm_private *priv = dev->dev_private;
  280. struct msm_file_private *ctx = file->driver_priv;
  281. struct msm_kms *kms = priv->kms;
  282. if (kms)
  283. kms->funcs->preclose(kms, file);
  284. mutex_lock(&dev->struct_mutex);
  285. if (ctx == priv->lastctx)
  286. priv->lastctx = NULL;
  287. mutex_unlock(&dev->struct_mutex);
  288. kfree(ctx);
  289. }
  290. static void msm_lastclose(struct drm_device *dev)
  291. {
  292. struct msm_drm_private *priv = dev->dev_private;
  293. if (priv->fbdev)
  294. drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
  295. }
  296. static irqreturn_t msm_irq(int irq, void *arg)
  297. {
  298. struct drm_device *dev = arg;
  299. struct msm_drm_private *priv = dev->dev_private;
  300. struct msm_kms *kms = priv->kms;
  301. BUG_ON(!kms);
  302. return kms->funcs->irq(kms);
  303. }
  304. static void msm_irq_preinstall(struct drm_device *dev)
  305. {
  306. struct msm_drm_private *priv = dev->dev_private;
  307. struct msm_kms *kms = priv->kms;
  308. BUG_ON(!kms);
  309. kms->funcs->irq_preinstall(kms);
  310. }
  311. static int msm_irq_postinstall(struct drm_device *dev)
  312. {
  313. struct msm_drm_private *priv = dev->dev_private;
  314. struct msm_kms *kms = priv->kms;
  315. BUG_ON(!kms);
  316. return kms->funcs->irq_postinstall(kms);
  317. }
  318. static void msm_irq_uninstall(struct drm_device *dev)
  319. {
  320. struct msm_drm_private *priv = dev->dev_private;
  321. struct msm_kms *kms = priv->kms;
  322. BUG_ON(!kms);
  323. kms->funcs->irq_uninstall(kms);
  324. }
  325. static int msm_enable_vblank(struct drm_device *dev, int crtc_id)
  326. {
  327. struct msm_drm_private *priv = dev->dev_private;
  328. struct msm_kms *kms = priv->kms;
  329. if (!kms)
  330. return -ENXIO;
  331. DBG("dev=%p, crtc=%d", dev, crtc_id);
  332. return kms->funcs->enable_vblank(kms, priv->crtcs[crtc_id]);
  333. }
  334. static void msm_disable_vblank(struct drm_device *dev, int crtc_id)
  335. {
  336. struct msm_drm_private *priv = dev->dev_private;
  337. struct msm_kms *kms = priv->kms;
  338. if (!kms)
  339. return;
  340. DBG("dev=%p, crtc=%d", dev, crtc_id);
  341. kms->funcs->disable_vblank(kms, priv->crtcs[crtc_id]);
  342. }
  343. /*
  344. * DRM debugfs:
  345. */
  346. #ifdef CONFIG_DEBUG_FS
  347. static int msm_gpu_show(struct drm_device *dev, struct seq_file *m)
  348. {
  349. struct msm_drm_private *priv = dev->dev_private;
  350. struct msm_gpu *gpu = priv->gpu;
  351. if (gpu) {
  352. seq_printf(m, "%s Status:\n", gpu->name);
  353. gpu->funcs->show(gpu, m);
  354. }
  355. return 0;
  356. }
  357. static int msm_gem_show(struct drm_device *dev, struct seq_file *m)
  358. {
  359. struct msm_drm_private *priv = dev->dev_private;
  360. struct msm_gpu *gpu = priv->gpu;
  361. if (gpu) {
  362. seq_printf(m, "Active Objects (%s):\n", gpu->name);
  363. msm_gem_describe_objects(&gpu->active_list, m);
  364. }
  365. seq_printf(m, "Inactive Objects:\n");
  366. msm_gem_describe_objects(&priv->inactive_list, m);
  367. return 0;
  368. }
  369. static int msm_mm_show(struct drm_device *dev, struct seq_file *m)
  370. {
  371. return drm_mm_dump_table(m, &dev->vma_offset_manager->vm_addr_space_mm);
  372. }
  373. static int msm_fb_show(struct drm_device *dev, struct seq_file *m)
  374. {
  375. struct msm_drm_private *priv = dev->dev_private;
  376. struct drm_framebuffer *fb, *fbdev_fb = NULL;
  377. if (priv->fbdev) {
  378. seq_printf(m, "fbcon ");
  379. fbdev_fb = priv->fbdev->fb;
  380. msm_framebuffer_describe(fbdev_fb, m);
  381. }
  382. mutex_lock(&dev->mode_config.fb_lock);
  383. list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
  384. if (fb == fbdev_fb)
  385. continue;
  386. seq_printf(m, "user ");
  387. msm_framebuffer_describe(fb, m);
  388. }
  389. mutex_unlock(&dev->mode_config.fb_lock);
  390. return 0;
  391. }
  392. static int show_locked(struct seq_file *m, void *arg)
  393. {
  394. struct drm_info_node *node = (struct drm_info_node *) m->private;
  395. struct drm_device *dev = node->minor->dev;
  396. int (*show)(struct drm_device *dev, struct seq_file *m) =
  397. node->info_ent->data;
  398. int ret;
  399. ret = mutex_lock_interruptible(&dev->struct_mutex);
  400. if (ret)
  401. return ret;
  402. ret = show(dev, m);
  403. mutex_unlock(&dev->struct_mutex);
  404. return ret;
  405. }
  406. static struct drm_info_list msm_debugfs_list[] = {
  407. {"gpu", show_locked, 0, msm_gpu_show},
  408. {"gem", show_locked, 0, msm_gem_show},
  409. { "mm", show_locked, 0, msm_mm_show },
  410. { "fb", show_locked, 0, msm_fb_show },
  411. };
  412. static int late_init_minor(struct drm_minor *minor)
  413. {
  414. int ret;
  415. if (!minor)
  416. return 0;
  417. ret = msm_rd_debugfs_init(minor);
  418. if (ret) {
  419. dev_err(minor->dev->dev, "could not install rd debugfs\n");
  420. return ret;
  421. }
  422. ret = msm_perf_debugfs_init(minor);
  423. if (ret) {
  424. dev_err(minor->dev->dev, "could not install perf debugfs\n");
  425. return ret;
  426. }
  427. return 0;
  428. }
  429. int msm_debugfs_late_init(struct drm_device *dev)
  430. {
  431. int ret;
  432. ret = late_init_minor(dev->primary);
  433. if (ret)
  434. return ret;
  435. ret = late_init_minor(dev->render);
  436. if (ret)
  437. return ret;
  438. ret = late_init_minor(dev->control);
  439. return ret;
  440. }
  441. static int msm_debugfs_init(struct drm_minor *minor)
  442. {
  443. struct drm_device *dev = minor->dev;
  444. int ret;
  445. ret = drm_debugfs_create_files(msm_debugfs_list,
  446. ARRAY_SIZE(msm_debugfs_list),
  447. minor->debugfs_root, minor);
  448. if (ret) {
  449. dev_err(dev->dev, "could not install msm_debugfs_list\n");
  450. return ret;
  451. }
  452. return 0;
  453. }
  454. static void msm_debugfs_cleanup(struct drm_minor *minor)
  455. {
  456. drm_debugfs_remove_files(msm_debugfs_list,
  457. ARRAY_SIZE(msm_debugfs_list), minor);
  458. if (!minor->dev->dev_private)
  459. return;
  460. msm_rd_debugfs_cleanup(minor);
  461. msm_perf_debugfs_cleanup(minor);
  462. }
  463. #endif
  464. /*
  465. * Fences:
  466. */
  467. int msm_wait_fence_interruptable(struct drm_device *dev, uint32_t fence,
  468. struct timespec *timeout)
  469. {
  470. struct msm_drm_private *priv = dev->dev_private;
  471. int ret;
  472. if (!priv->gpu)
  473. return 0;
  474. if (fence > priv->gpu->submitted_fence) {
  475. DRM_ERROR("waiting on invalid fence: %u (of %u)\n",
  476. fence, priv->gpu->submitted_fence);
  477. return -EINVAL;
  478. }
  479. if (!timeout) {
  480. /* no-wait: */
  481. ret = fence_completed(dev, fence) ? 0 : -EBUSY;
  482. } else {
  483. unsigned long timeout_jiffies = timespec_to_jiffies(timeout);
  484. unsigned long start_jiffies = jiffies;
  485. unsigned long remaining_jiffies;
  486. if (time_after(start_jiffies, timeout_jiffies))
  487. remaining_jiffies = 0;
  488. else
  489. remaining_jiffies = timeout_jiffies - start_jiffies;
  490. ret = wait_event_interruptible_timeout(priv->fence_event,
  491. fence_completed(dev, fence),
  492. remaining_jiffies);
  493. if (ret == 0) {
  494. DBG("timeout waiting for fence: %u (completed: %u)",
  495. fence, priv->completed_fence);
  496. ret = -ETIMEDOUT;
  497. } else if (ret != -ERESTARTSYS) {
  498. ret = 0;
  499. }
  500. }
  501. return ret;
  502. }
  503. /* called from workqueue */
  504. void msm_update_fence(struct drm_device *dev, uint32_t fence)
  505. {
  506. struct msm_drm_private *priv = dev->dev_private;
  507. mutex_lock(&dev->struct_mutex);
  508. priv->completed_fence = max(fence, priv->completed_fence);
  509. while (!list_empty(&priv->fence_cbs)) {
  510. struct msm_fence_cb *cb;
  511. cb = list_first_entry(&priv->fence_cbs,
  512. struct msm_fence_cb, work.entry);
  513. if (cb->fence > priv->completed_fence)
  514. break;
  515. list_del_init(&cb->work.entry);
  516. queue_work(priv->wq, &cb->work);
  517. }
  518. mutex_unlock(&dev->struct_mutex);
  519. wake_up_all(&priv->fence_event);
  520. }
  521. void __msm_fence_worker(struct work_struct *work)
  522. {
  523. struct msm_fence_cb *cb = container_of(work, struct msm_fence_cb, work);
  524. cb->func(cb);
  525. }
  526. /*
  527. * DRM ioctls:
  528. */
  529. static int msm_ioctl_get_param(struct drm_device *dev, void *data,
  530. struct drm_file *file)
  531. {
  532. struct msm_drm_private *priv = dev->dev_private;
  533. struct drm_msm_param *args = data;
  534. struct msm_gpu *gpu;
  535. /* for now, we just have 3d pipe.. eventually this would need to
  536. * be more clever to dispatch to appropriate gpu module:
  537. */
  538. if (args->pipe != MSM_PIPE_3D0)
  539. return -EINVAL;
  540. gpu = priv->gpu;
  541. if (!gpu)
  542. return -ENXIO;
  543. return gpu->funcs->get_param(gpu, args->param, &args->value);
  544. }
  545. static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
  546. struct drm_file *file)
  547. {
  548. struct drm_msm_gem_new *args = data;
  549. if (args->flags & ~MSM_BO_FLAGS) {
  550. DRM_ERROR("invalid flags: %08x\n", args->flags);
  551. return -EINVAL;
  552. }
  553. return msm_gem_new_handle(dev, file, args->size,
  554. args->flags, &args->handle);
  555. }
  556. #define TS(t) ((struct timespec){ .tv_sec = (t).tv_sec, .tv_nsec = (t).tv_nsec })
  557. static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
  558. struct drm_file *file)
  559. {
  560. struct drm_msm_gem_cpu_prep *args = data;
  561. struct drm_gem_object *obj;
  562. int ret;
  563. if (args->op & ~MSM_PREP_FLAGS) {
  564. DRM_ERROR("invalid op: %08x\n", args->op);
  565. return -EINVAL;
  566. }
  567. obj = drm_gem_object_lookup(dev, file, args->handle);
  568. if (!obj)
  569. return -ENOENT;
  570. ret = msm_gem_cpu_prep(obj, args->op, &TS(args->timeout));
  571. drm_gem_object_unreference_unlocked(obj);
  572. return ret;
  573. }
  574. static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
  575. struct drm_file *file)
  576. {
  577. struct drm_msm_gem_cpu_fini *args = data;
  578. struct drm_gem_object *obj;
  579. int ret;
  580. obj = drm_gem_object_lookup(dev, file, args->handle);
  581. if (!obj)
  582. return -ENOENT;
  583. ret = msm_gem_cpu_fini(obj);
  584. drm_gem_object_unreference_unlocked(obj);
  585. return ret;
  586. }
  587. static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
  588. struct drm_file *file)
  589. {
  590. struct drm_msm_gem_info *args = data;
  591. struct drm_gem_object *obj;
  592. int ret = 0;
  593. if (args->pad)
  594. return -EINVAL;
  595. obj = drm_gem_object_lookup(dev, file, args->handle);
  596. if (!obj)
  597. return -ENOENT;
  598. args->offset = msm_gem_mmap_offset(obj);
  599. drm_gem_object_unreference_unlocked(obj);
  600. return ret;
  601. }
  602. static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
  603. struct drm_file *file)
  604. {
  605. struct drm_msm_wait_fence *args = data;
  606. if (args->pad) {
  607. DRM_ERROR("invalid pad: %08x\n", args->pad);
  608. return -EINVAL;
  609. }
  610. return msm_wait_fence_interruptable(dev, args->fence,
  611. &TS(args->timeout));
  612. }
  613. static const struct drm_ioctl_desc msm_ioctls[] = {
  614. DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  615. DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  616. DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  617. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  618. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  619. DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  620. DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  621. };
  622. static const struct vm_operations_struct vm_ops = {
  623. .fault = msm_gem_fault,
  624. .open = drm_gem_vm_open,
  625. .close = drm_gem_vm_close,
  626. };
  627. static const struct file_operations fops = {
  628. .owner = THIS_MODULE,
  629. .open = drm_open,
  630. .release = drm_release,
  631. .unlocked_ioctl = drm_ioctl,
  632. #ifdef CONFIG_COMPAT
  633. .compat_ioctl = drm_compat_ioctl,
  634. #endif
  635. .poll = drm_poll,
  636. .read = drm_read,
  637. .llseek = no_llseek,
  638. .mmap = msm_gem_mmap,
  639. };
  640. static struct drm_driver msm_driver = {
  641. .driver_features = DRIVER_HAVE_IRQ |
  642. DRIVER_GEM |
  643. DRIVER_PRIME |
  644. DRIVER_RENDER |
  645. DRIVER_MODESET,
  646. .load = msm_load,
  647. .unload = msm_unload,
  648. .open = msm_open,
  649. .preclose = msm_preclose,
  650. .lastclose = msm_lastclose,
  651. .set_busid = drm_platform_set_busid,
  652. .irq_handler = msm_irq,
  653. .irq_preinstall = msm_irq_preinstall,
  654. .irq_postinstall = msm_irq_postinstall,
  655. .irq_uninstall = msm_irq_uninstall,
  656. .get_vblank_counter = drm_vblank_count,
  657. .enable_vblank = msm_enable_vblank,
  658. .disable_vblank = msm_disable_vblank,
  659. .gem_free_object = msm_gem_free_object,
  660. .gem_vm_ops = &vm_ops,
  661. .dumb_create = msm_gem_dumb_create,
  662. .dumb_map_offset = msm_gem_dumb_map_offset,
  663. .dumb_destroy = drm_gem_dumb_destroy,
  664. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  665. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  666. .gem_prime_export = drm_gem_prime_export,
  667. .gem_prime_import = drm_gem_prime_import,
  668. .gem_prime_pin = msm_gem_prime_pin,
  669. .gem_prime_unpin = msm_gem_prime_unpin,
  670. .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
  671. .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
  672. .gem_prime_vmap = msm_gem_prime_vmap,
  673. .gem_prime_vunmap = msm_gem_prime_vunmap,
  674. #ifdef CONFIG_DEBUG_FS
  675. .debugfs_init = msm_debugfs_init,
  676. .debugfs_cleanup = msm_debugfs_cleanup,
  677. #endif
  678. .ioctls = msm_ioctls,
  679. .num_ioctls = DRM_MSM_NUM_IOCTLS,
  680. .fops = &fops,
  681. .name = "msm",
  682. .desc = "MSM Snapdragon DRM",
  683. .date = "20130625",
  684. .major = 1,
  685. .minor = 0,
  686. };
  687. #ifdef CONFIG_PM_SLEEP
  688. static int msm_pm_suspend(struct device *dev)
  689. {
  690. struct drm_device *ddev = dev_get_drvdata(dev);
  691. drm_kms_helper_poll_disable(ddev);
  692. return 0;
  693. }
  694. static int msm_pm_resume(struct device *dev)
  695. {
  696. struct drm_device *ddev = dev_get_drvdata(dev);
  697. drm_kms_helper_poll_enable(ddev);
  698. return 0;
  699. }
  700. #endif
  701. static const struct dev_pm_ops msm_pm_ops = {
  702. SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
  703. };
  704. /*
  705. * Componentized driver support:
  706. */
  707. #ifdef CONFIG_OF
  708. /* NOTE: the CONFIG_OF case duplicates the same code as exynos or imx
  709. * (or probably any other).. so probably some room for some helpers
  710. */
  711. static int compare_of(struct device *dev, void *data)
  712. {
  713. return dev->of_node == data;
  714. }
  715. static int add_components(struct device *dev, struct component_match **matchptr,
  716. const char *name)
  717. {
  718. struct device_node *np = dev->of_node;
  719. unsigned i;
  720. for (i = 0; ; i++) {
  721. struct device_node *node;
  722. node = of_parse_phandle(np, name, i);
  723. if (!node)
  724. break;
  725. component_match_add(dev, matchptr, compare_of, node);
  726. }
  727. return 0;
  728. }
  729. #else
  730. static int compare_dev(struct device *dev, void *data)
  731. {
  732. return dev == data;
  733. }
  734. #endif
  735. static int msm_drm_bind(struct device *dev)
  736. {
  737. return drm_platform_init(&msm_driver, to_platform_device(dev));
  738. }
  739. static void msm_drm_unbind(struct device *dev)
  740. {
  741. drm_put_dev(platform_get_drvdata(to_platform_device(dev)));
  742. }
  743. static const struct component_master_ops msm_drm_ops = {
  744. .bind = msm_drm_bind,
  745. .unbind = msm_drm_unbind,
  746. };
  747. /*
  748. * Platform driver:
  749. */
  750. static int msm_pdev_probe(struct platform_device *pdev)
  751. {
  752. struct component_match *match = NULL;
  753. #ifdef CONFIG_OF
  754. add_components(&pdev->dev, &match, "connectors");
  755. add_components(&pdev->dev, &match, "gpus");
  756. #else
  757. /* For non-DT case, it kinda sucks. We don't actually have a way
  758. * to know whether or not we are waiting for certain devices (or if
  759. * they are simply not present). But for non-DT we only need to
  760. * care about apq8064/apq8060/etc (all mdp4/a3xx):
  761. */
  762. static const char *devnames[] = {
  763. "hdmi_msm.0", "kgsl-3d0.0",
  764. };
  765. int i;
  766. DBG("Adding components..");
  767. for (i = 0; i < ARRAY_SIZE(devnames); i++) {
  768. struct device *dev;
  769. dev = bus_find_device_by_name(&platform_bus_type,
  770. NULL, devnames[i]);
  771. if (!dev) {
  772. dev_info(&pdev->dev, "still waiting for %s\n", devnames[i]);
  773. return -EPROBE_DEFER;
  774. }
  775. component_match_add(&pdev->dev, &match, compare_dev, dev);
  776. }
  777. #endif
  778. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  779. return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
  780. }
  781. static int msm_pdev_remove(struct platform_device *pdev)
  782. {
  783. component_master_del(&pdev->dev, &msm_drm_ops);
  784. return 0;
  785. }
  786. static const struct platform_device_id msm_id[] = {
  787. { "mdp", 0 },
  788. { }
  789. };
  790. static const struct of_device_id dt_match[] = {
  791. { .compatible = "qcom,mdp" }, /* mdp4 */
  792. { .compatible = "qcom,mdss_mdp" }, /* mdp5 */
  793. {}
  794. };
  795. MODULE_DEVICE_TABLE(of, dt_match);
  796. static struct platform_driver msm_platform_driver = {
  797. .probe = msm_pdev_probe,
  798. .remove = msm_pdev_remove,
  799. .driver = {
  800. .owner = THIS_MODULE,
  801. .name = "msm",
  802. .of_match_table = dt_match,
  803. .pm = &msm_pm_ops,
  804. },
  805. .id_table = msm_id,
  806. };
  807. static int __init msm_drm_register(void)
  808. {
  809. DBG("init");
  810. hdmi_register();
  811. adreno_register();
  812. return platform_driver_register(&msm_platform_driver);
  813. }
  814. static void __exit msm_drm_unregister(void)
  815. {
  816. DBG("fini");
  817. platform_driver_unregister(&msm_platform_driver);
  818. hdmi_unregister();
  819. adreno_unregister();
  820. }
  821. module_init(msm_drm_register);
  822. module_exit(msm_drm_unregister);
  823. MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
  824. MODULE_DESCRIPTION("MSM DRM Driver");
  825. MODULE_LICENSE("GPL");