msm_drv.c 26 KB

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