msm_drv.c 25 KB

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