omap_drv.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /*
  2. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  3. * Author: Rob Clark <rob@ti.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 <linux/of.h>
  18. #include <linux/sort.h>
  19. #include <linux/sys_soc.h>
  20. #include <drm/drm_atomic.h>
  21. #include <drm/drm_atomic_helper.h>
  22. #include <drm/drm_crtc_helper.h>
  23. #include <drm/drm_fb_helper.h>
  24. #include "omap_dmm_tiler.h"
  25. #include "omap_drv.h"
  26. #define DRIVER_NAME MODULE_NAME
  27. #define DRIVER_DESC "OMAP DRM"
  28. #define DRIVER_DATE "20110917"
  29. #define DRIVER_MAJOR 1
  30. #define DRIVER_MINOR 0
  31. #define DRIVER_PATCHLEVEL 0
  32. /*
  33. * mode config funcs
  34. */
  35. /* Notes about mapping DSS and DRM entities:
  36. * CRTC: overlay
  37. * encoder: manager.. with some extension to allow one primary CRTC
  38. * and zero or more video CRTC's to be mapped to one encoder?
  39. * connector: dssdev.. manager can be attached/detached from different
  40. * devices
  41. */
  42. static void omap_atomic_wait_for_completion(struct drm_device *dev,
  43. struct drm_atomic_state *old_state)
  44. {
  45. struct drm_crtc_state *new_crtc_state;
  46. struct drm_crtc *crtc;
  47. unsigned int i;
  48. int ret;
  49. for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
  50. if (!new_crtc_state->active)
  51. continue;
  52. ret = omap_crtc_wait_pending(crtc);
  53. if (!ret)
  54. dev_warn(dev->dev,
  55. "atomic complete timeout (pipe %u)!\n", i);
  56. }
  57. }
  58. static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
  59. {
  60. struct drm_device *dev = old_state->dev;
  61. struct omap_drm_private *priv = dev->dev_private;
  62. priv->dispc_ops->runtime_get(priv->dispc);
  63. /* Apply the atomic update. */
  64. drm_atomic_helper_commit_modeset_disables(dev, old_state);
  65. if (priv->omaprev != 0x3430) {
  66. /* With the current dss dispc implementation we have to enable
  67. * the new modeset before we can commit planes. The dispc ovl
  68. * configuration relies on the video mode configuration been
  69. * written into the HW when the ovl configuration is
  70. * calculated.
  71. *
  72. * This approach is not ideal because after a mode change the
  73. * plane update is executed only after the first vblank
  74. * interrupt. The dispc implementation should be fixed so that
  75. * it is able use uncommitted drm state information.
  76. */
  77. drm_atomic_helper_commit_modeset_enables(dev, old_state);
  78. omap_atomic_wait_for_completion(dev, old_state);
  79. drm_atomic_helper_commit_planes(dev, old_state, 0);
  80. drm_atomic_helper_commit_hw_done(old_state);
  81. } else {
  82. /*
  83. * OMAP3 DSS seems to have issues with the work-around above,
  84. * resulting in endless sync losts if a crtc is enabled without
  85. * a plane. For now, skip the WA for OMAP3.
  86. */
  87. drm_atomic_helper_commit_planes(dev, old_state, 0);
  88. drm_atomic_helper_commit_modeset_enables(dev, old_state);
  89. drm_atomic_helper_commit_hw_done(old_state);
  90. }
  91. /*
  92. * Wait for completion of the page flips to ensure that old buffers
  93. * can't be touched by the hardware anymore before cleaning up planes.
  94. */
  95. omap_atomic_wait_for_completion(dev, old_state);
  96. drm_atomic_helper_cleanup_planes(dev, old_state);
  97. priv->dispc_ops->runtime_put(priv->dispc);
  98. }
  99. static int drm_atomic_state_normalized_zpos_cmp(const void *a, const void *b)
  100. {
  101. const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
  102. const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
  103. if (sa->normalized_zpos != sb->normalized_zpos)
  104. return sa->normalized_zpos - sb->normalized_zpos;
  105. else
  106. return sa->plane->base.id - sb->plane->base.id;
  107. }
  108. static int omap_atomic_update_normalize_zpos(struct drm_device *dev,
  109. struct drm_atomic_state *state)
  110. {
  111. struct drm_crtc *crtc;
  112. struct drm_crtc_state *old_state, *new_state;
  113. struct drm_plane *plane;
  114. int c, i, n, inc;
  115. int total_planes = dev->mode_config.num_total_plane;
  116. struct drm_plane_state **states;
  117. int ret = 0;
  118. states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
  119. if (!states)
  120. return -ENOMEM;
  121. for_each_oldnew_crtc_in_state(state, crtc, old_state, new_state, c) {
  122. if (old_state->plane_mask == new_state->plane_mask &&
  123. !new_state->zpos_changed)
  124. continue;
  125. /* Reset plane increment and index value for every crtc */
  126. n = 0;
  127. /*
  128. * Normalization process might create new states for planes
  129. * which normalized_zpos has to be recalculated.
  130. */
  131. drm_for_each_plane_mask(plane, dev, new_state->plane_mask) {
  132. struct drm_plane_state *plane_state =
  133. drm_atomic_get_plane_state(new_state->state,
  134. plane);
  135. if (IS_ERR(plane_state)) {
  136. ret = PTR_ERR(plane_state);
  137. goto done;
  138. }
  139. states[n++] = plane_state;
  140. }
  141. sort(states, n, sizeof(*states),
  142. drm_atomic_state_normalized_zpos_cmp, NULL);
  143. for (i = 0, inc = 0; i < n; i++) {
  144. plane = states[i]->plane;
  145. states[i]->normalized_zpos = i + inc;
  146. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] updated normalized zpos value %d\n",
  147. plane->base.id, plane->name,
  148. states[i]->normalized_zpos);
  149. if (is_omap_plane_dual_overlay(states[i]))
  150. inc++;
  151. }
  152. new_state->zpos_changed = true;
  153. }
  154. done:
  155. kfree(states);
  156. return ret;
  157. }
  158. static int omap_atomic_check(struct drm_device *dev,
  159. struct drm_atomic_state *state)
  160. {
  161. int ret;
  162. ret = drm_atomic_helper_check(dev, state);
  163. if (ret)
  164. return ret;
  165. if (dev->mode_config.normalize_zpos) {
  166. ret = omap_atomic_update_normalize_zpos(dev, state);
  167. if (ret)
  168. return ret;
  169. }
  170. return 0;
  171. }
  172. static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
  173. .atomic_commit_tail = omap_atomic_commit_tail,
  174. };
  175. static const struct drm_mode_config_funcs omap_mode_config_funcs = {
  176. .fb_create = omap_framebuffer_create,
  177. .output_poll_changed = drm_fb_helper_output_poll_changed,
  178. .atomic_check = omap_atomic_check,
  179. .atomic_commit = drm_atomic_helper_commit,
  180. };
  181. /* Global/shared object state funcs */
  182. /*
  183. * This is a helper that returns the private state currently in operation.
  184. * Note that this would return the "old_state" if called in the atomic check
  185. * path, and the "new_state" after the atomic swap has been done.
  186. */
  187. struct omap_global_state *
  188. omap_get_existing_global_state(struct omap_drm_private *priv)
  189. {
  190. return to_omap_global_state(priv->glob_obj.state);
  191. }
  192. /*
  193. * This acquires the modeset lock set aside for global state, creates
  194. * a new duplicated private object state.
  195. */
  196. struct omap_global_state *__must_check
  197. omap_get_global_state(struct drm_atomic_state *s)
  198. {
  199. struct omap_drm_private *priv = s->dev->dev_private;
  200. struct drm_private_state *priv_state;
  201. int ret;
  202. if (!drm_modeset_is_locked(&priv->glob_obj_lock)) {
  203. ret = drm_modeset_lock(&priv->glob_obj_lock, s->acquire_ctx);
  204. if (ret) {
  205. DBG("getting priv->glob_obj_lock (%p) failed %d",
  206. &priv->glob_obj_lock, ret);
  207. return ERR_PTR(ret);
  208. }
  209. }
  210. priv_state = drm_atomic_get_private_obj_state(s, &priv->glob_obj);
  211. if (IS_ERR(priv_state))
  212. return ERR_CAST(priv_state);
  213. return to_omap_global_state(priv_state);
  214. }
  215. static struct drm_private_state *
  216. omap_global_duplicate_state(struct drm_private_obj *obj)
  217. {
  218. struct omap_global_state *state;
  219. state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
  220. if (!state)
  221. return NULL;
  222. __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
  223. return &state->base;
  224. }
  225. static void omap_global_destroy_state(struct drm_private_obj *obj,
  226. struct drm_private_state *state)
  227. {
  228. struct omap_global_state *omap_state = to_omap_global_state(state);
  229. kfree(omap_state);
  230. }
  231. static const struct drm_private_state_funcs omap_global_state_funcs = {
  232. .atomic_duplicate_state = omap_global_duplicate_state,
  233. .atomic_destroy_state = omap_global_destroy_state,
  234. };
  235. static int omap_global_obj_init(struct omap_drm_private *priv)
  236. {
  237. struct omap_global_state *state;
  238. drm_modeset_lock_init(&priv->glob_obj_lock);
  239. state = kzalloc(sizeof(*state), GFP_KERNEL);
  240. if (!state)
  241. return -ENOMEM;
  242. drm_atomic_private_obj_init(&priv->glob_obj, &state->base,
  243. &omap_global_state_funcs);
  244. return 0;
  245. }
  246. static void omap_global_obj_fini(struct omap_drm_private *priv)
  247. {
  248. drm_atomic_private_obj_fini(&priv->glob_obj);
  249. drm_modeset_lock_fini(&priv->glob_obj_lock);
  250. }
  251. static void omap_disconnect_pipelines(struct drm_device *ddev)
  252. {
  253. struct omap_drm_private *priv = ddev->dev_private;
  254. unsigned int i;
  255. for (i = 0; i < priv->num_pipes; i++) {
  256. struct omap_drm_pipeline *pipe = &priv->pipes[i];
  257. omapdss_device_disconnect(NULL, pipe->output);
  258. omapdss_device_put(pipe->output);
  259. omapdss_device_put(pipe->display);
  260. pipe->output = NULL;
  261. pipe->display = NULL;
  262. }
  263. memset(&priv->channels, 0, sizeof(priv->channels));
  264. priv->num_pipes = 0;
  265. }
  266. static int omap_compare_pipes(const void *a, const void *b)
  267. {
  268. const struct omap_drm_pipeline *pipe1 = a;
  269. const struct omap_drm_pipeline *pipe2 = b;
  270. if (pipe1->display->alias_id > pipe2->display->alias_id)
  271. return 1;
  272. else if (pipe1->display->alias_id < pipe2->display->alias_id)
  273. return -1;
  274. return 0;
  275. }
  276. static int omap_connect_pipelines(struct drm_device *ddev)
  277. {
  278. struct omap_drm_private *priv = ddev->dev_private;
  279. struct omap_dss_device *output = NULL;
  280. unsigned int i;
  281. int r;
  282. if (!omapdss_stack_is_ready())
  283. return -EPROBE_DEFER;
  284. for_each_dss_output(output) {
  285. r = omapdss_device_connect(priv->dss, NULL, output);
  286. if (r == -EPROBE_DEFER) {
  287. omapdss_device_put(output);
  288. goto cleanup;
  289. } else if (r) {
  290. dev_warn(output->dev, "could not connect output %s\n",
  291. output->name);
  292. } else {
  293. struct omap_drm_pipeline *pipe;
  294. pipe = &priv->pipes[priv->num_pipes++];
  295. pipe->output = omapdss_device_get(output);
  296. pipe->display = omapdss_display_get(output);
  297. if (priv->num_pipes == ARRAY_SIZE(priv->pipes)) {
  298. /* To balance the 'for_each_dss_output' loop */
  299. omapdss_device_put(output);
  300. break;
  301. }
  302. }
  303. }
  304. /* Sort the list by DT aliases */
  305. sort(priv->pipes, priv->num_pipes, sizeof(priv->pipes[0]),
  306. omap_compare_pipes, NULL);
  307. /*
  308. * Populate the pipeline lookup table by DISPC channel. Only one display
  309. * is allowed per channel.
  310. */
  311. for (i = 0; i < priv->num_pipes; ++i) {
  312. struct omap_drm_pipeline *pipe = &priv->pipes[i];
  313. enum omap_channel channel = pipe->output->dispc_channel;
  314. if (WARN_ON(priv->channels[channel] != NULL)) {
  315. r = -EINVAL;
  316. goto cleanup;
  317. }
  318. priv->channels[channel] = pipe;
  319. }
  320. return 0;
  321. cleanup:
  322. /*
  323. * if we are deferring probe, we disconnect the devices we previously
  324. * connected
  325. */
  326. omap_disconnect_pipelines(ddev);
  327. return r;
  328. }
  329. static int omap_modeset_init_properties(struct drm_device *dev)
  330. {
  331. struct omap_drm_private *priv = dev->dev_private;
  332. unsigned int num_planes = priv->dispc_ops->get_num_ovls(priv->dispc);
  333. priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
  334. num_planes - 1);
  335. if (!priv->zorder_prop)
  336. return -ENOMEM;
  337. return 0;
  338. }
  339. static int omap_modeset_init(struct drm_device *dev)
  340. {
  341. struct omap_drm_private *priv = dev->dev_private;
  342. int num_ovls = priv->dispc_ops->get_num_ovls(priv->dispc);
  343. int num_mgrs = priv->dispc_ops->get_num_mgrs(priv->dispc);
  344. unsigned int i;
  345. int ret;
  346. u32 plane_crtc_mask;
  347. drm_mode_config_init(dev);
  348. ret = omap_modeset_init_properties(dev);
  349. if (ret < 0)
  350. return ret;
  351. /*
  352. * This function creates exactly one connector, encoder, crtc,
  353. * and primary plane per each connected dss-device. Each
  354. * connector->encoder->crtc chain is expected to be separate
  355. * and each crtc is connect to a single dss-channel. If the
  356. * configuration does not match the expectations or exceeds
  357. * the available resources, the configuration is rejected.
  358. */
  359. if (priv->num_pipes > num_mgrs || priv->num_pipes > num_ovls) {
  360. dev_err(dev->dev, "%s(): Too many connected displays\n",
  361. __func__);
  362. return -EINVAL;
  363. }
  364. /* Create all planes first. They can all be put to any CRTC. */
  365. plane_crtc_mask = (1 << priv->num_pipes) - 1;
  366. for (i = 0; i < num_ovls; i++) {
  367. enum drm_plane_type type = i < priv->num_pipes
  368. ? DRM_PLANE_TYPE_PRIMARY
  369. : DRM_PLANE_TYPE_OVERLAY;
  370. struct drm_plane *plane;
  371. if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
  372. return -EINVAL;
  373. plane = omap_plane_init(dev, i, type, plane_crtc_mask);
  374. if (IS_ERR(plane))
  375. return PTR_ERR(plane);
  376. priv->planes[priv->num_planes++] = plane;
  377. }
  378. /* Create the CRTCs, encoders and connectors. */
  379. for (i = 0; i < priv->num_pipes; i++) {
  380. struct omap_drm_pipeline *pipe = &priv->pipes[i];
  381. struct omap_dss_device *display = pipe->display;
  382. struct drm_connector *connector;
  383. struct drm_encoder *encoder;
  384. struct drm_crtc *crtc;
  385. encoder = omap_encoder_init(dev, pipe->output, display);
  386. if (!encoder)
  387. return -ENOMEM;
  388. connector = omap_connector_init(dev, pipe->output, display,
  389. encoder);
  390. if (!connector)
  391. return -ENOMEM;
  392. crtc = omap_crtc_init(dev, pipe, priv->planes[i]);
  393. if (IS_ERR(crtc))
  394. return PTR_ERR(crtc);
  395. drm_connector_attach_encoder(connector, encoder);
  396. encoder->possible_crtcs = 1 << i;
  397. pipe->crtc = crtc;
  398. pipe->encoder = encoder;
  399. pipe->connector = connector;
  400. }
  401. DBG("registered %u planes, %u crtcs/encoders/connectors\n",
  402. priv->num_planes, priv->num_pipes);
  403. dev->mode_config.min_width = 8;
  404. dev->mode_config.min_height = 2;
  405. /*
  406. * Note: these values are used for multiple independent things:
  407. * connector mode filtering, buffer sizes, crtc sizes...
  408. * Use big enough values here to cover all use cases, and do more
  409. * specific checking in the respective code paths.
  410. */
  411. dev->mode_config.max_width = 8192;
  412. dev->mode_config.max_height = 8192;
  413. /* We want the zpos to be normalized */
  414. dev->mode_config.normalize_zpos = true;
  415. dev->mode_config.funcs = &omap_mode_config_funcs;
  416. dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
  417. drm_mode_config_reset(dev);
  418. omap_drm_irq_install(dev);
  419. return 0;
  420. }
  421. /*
  422. * Enable the HPD in external components if supported
  423. */
  424. static void omap_modeset_enable_external_hpd(struct drm_device *ddev)
  425. {
  426. struct omap_drm_private *priv = ddev->dev_private;
  427. int i;
  428. for (i = 0; i < priv->num_pipes; i++)
  429. omap_connector_enable_hpd(priv->pipes[i].connector);
  430. }
  431. /*
  432. * Disable the HPD in external components if supported
  433. */
  434. static void omap_modeset_disable_external_hpd(struct drm_device *ddev)
  435. {
  436. struct omap_drm_private *priv = ddev->dev_private;
  437. int i;
  438. for (i = 0; i < priv->num_pipes; i++)
  439. omap_connector_disable_hpd(priv->pipes[i].connector);
  440. }
  441. /*
  442. * drm ioctl funcs
  443. */
  444. static int ioctl_get_param(struct drm_device *dev, void *data,
  445. struct drm_file *file_priv)
  446. {
  447. struct omap_drm_private *priv = dev->dev_private;
  448. struct drm_omap_param *args = data;
  449. DBG("%p: param=%llu", dev, args->param);
  450. switch (args->param) {
  451. case OMAP_PARAM_CHIPSET_ID:
  452. args->value = priv->omaprev;
  453. break;
  454. default:
  455. DBG("unknown parameter %lld", args->param);
  456. return -EINVAL;
  457. }
  458. return 0;
  459. }
  460. static int ioctl_set_param(struct drm_device *dev, void *data,
  461. struct drm_file *file_priv)
  462. {
  463. struct drm_omap_param *args = data;
  464. switch (args->param) {
  465. default:
  466. DBG("unknown parameter %lld", args->param);
  467. return -EINVAL;
  468. }
  469. return 0;
  470. }
  471. #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
  472. static int ioctl_gem_new(struct drm_device *dev, void *data,
  473. struct drm_file *file_priv)
  474. {
  475. struct drm_omap_gem_new *args = data;
  476. u32 flags = args->flags & OMAP_BO_USER_MASK;
  477. VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
  478. args->size.bytes, flags);
  479. return omap_gem_new_handle(dev, file_priv, args->size, flags,
  480. &args->handle);
  481. }
  482. static int ioctl_gem_info(struct drm_device *dev, void *data,
  483. struct drm_file *file_priv)
  484. {
  485. struct drm_omap_gem_info *args = data;
  486. struct drm_gem_object *obj;
  487. int ret = 0;
  488. VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
  489. obj = drm_gem_object_lookup(file_priv, args->handle);
  490. if (!obj)
  491. return -ENOENT;
  492. args->size = omap_gem_mmap_size(obj);
  493. args->offset = omap_gem_mmap_offset(obj);
  494. drm_gem_object_put_unlocked(obj);
  495. return ret;
  496. }
  497. static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
  498. DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
  499. DRM_AUTH | DRM_RENDER_ALLOW),
  500. DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param,
  501. DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
  502. DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
  503. DRM_AUTH | DRM_RENDER_ALLOW),
  504. /* Deprecated, to be removed. */
  505. DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
  506. DRM_AUTH | DRM_RENDER_ALLOW),
  507. /* Deprecated, to be removed. */
  508. DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
  509. DRM_AUTH | DRM_RENDER_ALLOW),
  510. DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
  511. DRM_AUTH | DRM_RENDER_ALLOW),
  512. };
  513. /*
  514. * drm driver funcs
  515. */
  516. static int dev_open(struct drm_device *dev, struct drm_file *file)
  517. {
  518. file->driver_priv = NULL;
  519. DBG("open: dev=%p, file=%p", dev, file);
  520. return 0;
  521. }
  522. static const struct vm_operations_struct omap_gem_vm_ops = {
  523. .fault = omap_gem_fault,
  524. .open = drm_gem_vm_open,
  525. .close = drm_gem_vm_close,
  526. };
  527. static const struct file_operations omapdriver_fops = {
  528. .owner = THIS_MODULE,
  529. .open = drm_open,
  530. .unlocked_ioctl = drm_ioctl,
  531. .compat_ioctl = drm_compat_ioctl,
  532. .release = drm_release,
  533. .mmap = omap_gem_mmap,
  534. .poll = drm_poll,
  535. .read = drm_read,
  536. .llseek = noop_llseek,
  537. };
  538. static struct drm_driver omap_drm_driver = {
  539. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
  540. DRIVER_ATOMIC | DRIVER_RENDER,
  541. .open = dev_open,
  542. .lastclose = drm_fb_helper_lastclose,
  543. #ifdef CONFIG_DEBUG_FS
  544. .debugfs_init = omap_debugfs_init,
  545. #endif
  546. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  547. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  548. .gem_prime_export = omap_gem_prime_export,
  549. .gem_prime_import = omap_gem_prime_import,
  550. .gem_free_object_unlocked = omap_gem_free_object,
  551. .gem_vm_ops = &omap_gem_vm_ops,
  552. .dumb_create = omap_gem_dumb_create,
  553. .dumb_map_offset = omap_gem_dumb_map_offset,
  554. .ioctls = ioctls,
  555. .num_ioctls = DRM_OMAP_NUM_IOCTLS,
  556. .fops = &omapdriver_fops,
  557. .name = DRIVER_NAME,
  558. .desc = DRIVER_DESC,
  559. .date = DRIVER_DATE,
  560. .major = DRIVER_MAJOR,
  561. .minor = DRIVER_MINOR,
  562. .patchlevel = DRIVER_PATCHLEVEL,
  563. };
  564. static const struct soc_device_attribute omapdrm_soc_devices[] = {
  565. { .family = "OMAP3", .data = (void *)0x3430 },
  566. { .family = "OMAP4", .data = (void *)0x4430 },
  567. { .family = "OMAP5", .data = (void *)0x5430 },
  568. { .family = "DRA7", .data = (void *)0x0752 },
  569. { /* sentinel */ }
  570. };
  571. static int omapdrm_init(struct omap_drm_private *priv, struct device *dev)
  572. {
  573. const struct soc_device_attribute *soc;
  574. struct drm_device *ddev;
  575. unsigned int i;
  576. int ret;
  577. DBG("%s", dev_name(dev));
  578. /* Allocate and initialize the DRM device. */
  579. ddev = drm_dev_alloc(&omap_drm_driver, dev);
  580. if (IS_ERR(ddev))
  581. return PTR_ERR(ddev);
  582. priv->ddev = ddev;
  583. ddev->dev_private = priv;
  584. priv->dev = dev;
  585. priv->dss = omapdss_get_dss();
  586. priv->dispc = dispc_get_dispc(priv->dss);
  587. priv->dispc_ops = dispc_get_ops(priv->dss);
  588. omap_crtc_pre_init(priv);
  589. ret = omap_connect_pipelines(ddev);
  590. if (ret)
  591. goto err_crtc_uninit;
  592. soc = soc_device_match(omapdrm_soc_devices);
  593. priv->omaprev = soc ? (unsigned int)soc->data : 0;
  594. priv->wq = alloc_ordered_workqueue("omapdrm", 0);
  595. mutex_init(&priv->list_lock);
  596. INIT_LIST_HEAD(&priv->obj_list);
  597. /* Get memory bandwidth limits */
  598. if (priv->dispc_ops->get_memory_bandwidth_limit)
  599. priv->max_bandwidth =
  600. priv->dispc_ops->get_memory_bandwidth_limit(priv->dispc);
  601. omap_gem_init(ddev);
  602. ret = omap_global_obj_init(priv);
  603. if (ret)
  604. goto err_gem_deinit;
  605. ret = omap_hwoverlays_init(priv);
  606. if (ret)
  607. goto err_free_priv_obj;
  608. ret = omap_modeset_init(ddev);
  609. if (ret) {
  610. dev_err(priv->dev, "omap_modeset_init failed: ret=%d\n", ret);
  611. goto err_free_overlays;
  612. }
  613. /* Initialize vblank handling, start with all CRTCs disabled. */
  614. ret = drm_vblank_init(ddev, priv->num_pipes);
  615. if (ret) {
  616. dev_err(priv->dev, "could not init vblank\n");
  617. goto err_cleanup_modeset;
  618. }
  619. for (i = 0; i < priv->num_pipes; i++)
  620. drm_crtc_vblank_off(priv->pipes[i].crtc);
  621. omap_fbdev_init(ddev);
  622. drm_kms_helper_poll_init(ddev);
  623. omap_modeset_enable_external_hpd(ddev);
  624. /*
  625. * Register the DRM device with the core and the connectors with
  626. * sysfs.
  627. */
  628. ret = drm_dev_register(ddev, 0);
  629. if (ret)
  630. goto err_cleanup_helpers;
  631. return 0;
  632. err_cleanup_helpers:
  633. omap_modeset_disable_external_hpd(ddev);
  634. drm_kms_helper_poll_fini(ddev);
  635. omap_fbdev_fini(ddev);
  636. err_cleanup_modeset:
  637. drm_mode_config_cleanup(ddev);
  638. omap_drm_irq_uninstall(ddev);
  639. err_free_priv_obj:
  640. omap_global_obj_fini(priv);
  641. err_free_overlays:
  642. omap_hwoverlays_destroy(priv);
  643. err_gem_deinit:
  644. omap_gem_deinit(ddev);
  645. destroy_workqueue(priv->wq);
  646. omap_disconnect_pipelines(ddev);
  647. err_crtc_uninit:
  648. omap_crtc_pre_uninit(priv);
  649. drm_dev_put(ddev);
  650. return ret;
  651. }
  652. static void omapdrm_cleanup(struct omap_drm_private *priv)
  653. {
  654. struct drm_device *ddev = priv->ddev;
  655. DBG("");
  656. drm_dev_unregister(ddev);
  657. omap_modeset_disable_external_hpd(ddev);
  658. drm_kms_helper_poll_fini(ddev);
  659. omap_fbdev_fini(ddev);
  660. drm_atomic_helper_shutdown(ddev);
  661. drm_mode_config_cleanup(ddev);
  662. omap_drm_irq_uninstall(ddev);
  663. omap_gem_deinit(ddev);
  664. omap_global_obj_fini(priv);
  665. omap_hwoverlays_destroy(priv);
  666. destroy_workqueue(priv->wq);
  667. omap_disconnect_pipelines(ddev);
  668. omap_crtc_pre_uninit(priv);
  669. drm_dev_put(ddev);
  670. }
  671. static int pdev_probe(struct platform_device *pdev)
  672. {
  673. struct omap_drm_private *priv;
  674. int ret;
  675. if (omapdss_is_initialized() == false)
  676. return -EPROBE_DEFER;
  677. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  678. if (ret) {
  679. dev_err(&pdev->dev, "Failed to set the DMA mask\n");
  680. return ret;
  681. }
  682. /* Allocate and initialize the driver private structure. */
  683. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  684. if (!priv)
  685. return -ENOMEM;
  686. platform_set_drvdata(pdev, priv);
  687. ret = omapdrm_init(priv, &pdev->dev);
  688. if (ret < 0)
  689. kfree(priv);
  690. return ret;
  691. }
  692. static int pdev_remove(struct platform_device *pdev)
  693. {
  694. struct omap_drm_private *priv = platform_get_drvdata(pdev);
  695. omapdrm_cleanup(priv);
  696. kfree(priv);
  697. return 0;
  698. }
  699. #ifdef CONFIG_PM_SLEEP
  700. static int omap_drm_suspend_all_displays(struct drm_device *ddev)
  701. {
  702. struct omap_drm_private *priv = ddev->dev_private;
  703. int i;
  704. for (i = 0; i < priv->num_pipes; i++) {
  705. struct omap_dss_device *display = priv->pipes[i].display;
  706. if (display->state == OMAP_DSS_DISPLAY_ACTIVE) {
  707. display->ops->disable(display);
  708. display->activate_after_resume = true;
  709. } else {
  710. display->activate_after_resume = false;
  711. }
  712. }
  713. return 0;
  714. }
  715. static int omap_drm_resume_all_displays(struct drm_device *ddev)
  716. {
  717. struct omap_drm_private *priv = ddev->dev_private;
  718. int i;
  719. for (i = 0; i < priv->num_pipes; i++) {
  720. struct omap_dss_device *display = priv->pipes[i].display;
  721. if (display->activate_after_resume) {
  722. display->ops->enable(display);
  723. display->activate_after_resume = false;
  724. }
  725. }
  726. return 0;
  727. }
  728. static int omap_drm_suspend(struct device *dev)
  729. {
  730. struct omap_drm_private *priv = dev_get_drvdata(dev);
  731. struct drm_device *drm_dev = priv->ddev;
  732. drm_kms_helper_poll_disable(drm_dev);
  733. drm_modeset_lock_all(drm_dev);
  734. omap_drm_suspend_all_displays(drm_dev);
  735. drm_modeset_unlock_all(drm_dev);
  736. return 0;
  737. }
  738. static int omap_drm_resume(struct device *dev)
  739. {
  740. struct omap_drm_private *priv = dev_get_drvdata(dev);
  741. struct drm_device *drm_dev = priv->ddev;
  742. drm_modeset_lock_all(drm_dev);
  743. omap_drm_resume_all_displays(drm_dev);
  744. drm_modeset_unlock_all(drm_dev);
  745. drm_kms_helper_poll_enable(drm_dev);
  746. return omap_gem_resume(drm_dev);
  747. }
  748. #endif
  749. static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
  750. static struct platform_driver pdev = {
  751. .driver = {
  752. .name = "omapdrm",
  753. .pm = &omapdrm_pm_ops,
  754. },
  755. .probe = pdev_probe,
  756. .remove = pdev_remove,
  757. };
  758. static struct platform_driver * const drivers[] = {
  759. &omap_dmm_driver,
  760. &pdev,
  761. };
  762. static int __init omap_drm_init(void)
  763. {
  764. DBG("init");
  765. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  766. }
  767. static void __exit omap_drm_fini(void)
  768. {
  769. DBG("fini");
  770. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  771. }
  772. /* need late_initcall() so we load after dss_driver's are loaded */
  773. late_initcall(omap_drm_init);
  774. module_exit(omap_drm_fini);
  775. MODULE_AUTHOR("Rob Clark <rob@ti.com>");
  776. MODULE_DESCRIPTION("OMAP DRM Display Driver");
  777. MODULE_ALIAS("platform:" DRIVER_NAME);
  778. MODULE_LICENSE("GPL v2");