msm_drv.c 27 KB

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