cirrus_mode.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * Copyright 2012 Red Hat
  3. *
  4. * This file is subject to the terms and conditions of the GNU General
  5. * Public License version 2. See the file COPYING in the main
  6. * directory of this archive for more details.
  7. *
  8. * Authors: Matthew Garrett
  9. * Dave Airlie
  10. *
  11. * Portions of this code derived from cirrusfb.c:
  12. * drivers/video/cirrusfb.c - driver for Cirrus Logic chipsets
  13. *
  14. * Copyright 1999-2001 Jeff Garzik <jgarzik@pobox.com>
  15. */
  16. #include <drm/drmP.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/drm_plane_helper.h>
  19. #include <video/cirrus.h>
  20. #include "cirrus_drv.h"
  21. #define CIRRUS_LUT_SIZE 256
  22. #define PALETTE_INDEX 0x8
  23. #define PALETTE_DATA 0x9
  24. /*
  25. * This file contains setup code for the CRTC.
  26. */
  27. static void cirrus_crtc_load_lut(struct drm_crtc *crtc)
  28. {
  29. struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
  30. struct drm_device *dev = crtc->dev;
  31. struct cirrus_device *cdev = dev->dev_private;
  32. int i;
  33. if (!crtc->enabled)
  34. return;
  35. for (i = 0; i < CIRRUS_LUT_SIZE; i++) {
  36. /* VGA registers */
  37. WREG8(PALETTE_INDEX, i);
  38. WREG8(PALETTE_DATA, cirrus_crtc->lut_r[i]);
  39. WREG8(PALETTE_DATA, cirrus_crtc->lut_g[i]);
  40. WREG8(PALETTE_DATA, cirrus_crtc->lut_b[i]);
  41. }
  42. }
  43. /*
  44. * The DRM core requires DPMS functions, but they make little sense in our
  45. * case and so are just stubs
  46. */
  47. static void cirrus_crtc_dpms(struct drm_crtc *crtc, int mode)
  48. {
  49. struct drm_device *dev = crtc->dev;
  50. struct cirrus_device *cdev = dev->dev_private;
  51. u8 sr01, gr0e;
  52. switch (mode) {
  53. case DRM_MODE_DPMS_ON:
  54. sr01 = 0x00;
  55. gr0e = 0x00;
  56. break;
  57. case DRM_MODE_DPMS_STANDBY:
  58. sr01 = 0x20;
  59. gr0e = 0x02;
  60. break;
  61. case DRM_MODE_DPMS_SUSPEND:
  62. sr01 = 0x20;
  63. gr0e = 0x04;
  64. break;
  65. case DRM_MODE_DPMS_OFF:
  66. sr01 = 0x20;
  67. gr0e = 0x06;
  68. break;
  69. default:
  70. return;
  71. }
  72. WREG8(SEQ_INDEX, 0x1);
  73. sr01 |= RREG8(SEQ_DATA) & ~0x20;
  74. WREG_SEQ(0x1, sr01);
  75. WREG8(GFX_INDEX, 0xe);
  76. gr0e |= RREG8(GFX_DATA) & ~0x06;
  77. WREG_GFX(0xe, gr0e);
  78. }
  79. static void cirrus_set_start_address(struct drm_crtc *crtc, unsigned offset)
  80. {
  81. struct cirrus_device *cdev = crtc->dev->dev_private;
  82. u32 addr;
  83. u8 tmp;
  84. addr = offset >> 2;
  85. WREG_CRT(0x0c, (u8)((addr >> 8) & 0xff));
  86. WREG_CRT(0x0d, (u8)(addr & 0xff));
  87. WREG8(CRT_INDEX, 0x1b);
  88. tmp = RREG8(CRT_DATA);
  89. tmp &= 0xf2;
  90. tmp |= (addr >> 16) & 0x01;
  91. tmp |= (addr >> 15) & 0x0c;
  92. WREG_CRT(0x1b, tmp);
  93. WREG8(CRT_INDEX, 0x1d);
  94. tmp = RREG8(CRT_DATA);
  95. tmp &= 0x7f;
  96. tmp |= (addr >> 12) & 0x80;
  97. WREG_CRT(0x1d, tmp);
  98. }
  99. /* cirrus is different - we will force move buffers out of VRAM */
  100. static int cirrus_crtc_do_set_base(struct drm_crtc *crtc,
  101. struct drm_framebuffer *fb,
  102. int x, int y, int atomic)
  103. {
  104. struct cirrus_device *cdev = crtc->dev->dev_private;
  105. struct drm_gem_object *obj;
  106. struct cirrus_framebuffer *cirrus_fb;
  107. struct cirrus_bo *bo;
  108. int ret;
  109. u64 gpu_addr;
  110. /* push the previous fb to system ram */
  111. if (!atomic && fb) {
  112. cirrus_fb = to_cirrus_framebuffer(fb);
  113. obj = cirrus_fb->obj;
  114. bo = gem_to_cirrus_bo(obj);
  115. ret = cirrus_bo_reserve(bo, false);
  116. if (ret)
  117. return ret;
  118. cirrus_bo_push_sysram(bo);
  119. cirrus_bo_unreserve(bo);
  120. }
  121. cirrus_fb = to_cirrus_framebuffer(crtc->primary->fb);
  122. obj = cirrus_fb->obj;
  123. bo = gem_to_cirrus_bo(obj);
  124. ret = cirrus_bo_reserve(bo, false);
  125. if (ret)
  126. return ret;
  127. ret = cirrus_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
  128. if (ret) {
  129. cirrus_bo_unreserve(bo);
  130. return ret;
  131. }
  132. if (&cdev->mode_info.gfbdev->gfb == cirrus_fb) {
  133. /* if pushing console in kmap it */
  134. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  135. if (ret)
  136. DRM_ERROR("failed to kmap fbcon\n");
  137. }
  138. cirrus_bo_unreserve(bo);
  139. cirrus_set_start_address(crtc, (u32)gpu_addr);
  140. return 0;
  141. }
  142. static int cirrus_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  143. struct drm_framebuffer *old_fb)
  144. {
  145. return cirrus_crtc_do_set_base(crtc, old_fb, x, y, 0);
  146. }
  147. /*
  148. * The meat of this driver. The core passes us a mode and we have to program
  149. * it. The modesetting here is the bare minimum required to satisfy the qemu
  150. * emulation of this hardware, and running this against a real device is
  151. * likely to result in an inadequately programmed mode. We've already had
  152. * the opportunity to modify the mode, so whatever we receive here should
  153. * be something that can be correctly programmed and displayed
  154. */
  155. static int cirrus_crtc_mode_set(struct drm_crtc *crtc,
  156. struct drm_display_mode *mode,
  157. struct drm_display_mode *adjusted_mode,
  158. int x, int y, struct drm_framebuffer *old_fb)
  159. {
  160. struct drm_device *dev = crtc->dev;
  161. struct cirrus_device *cdev = dev->dev_private;
  162. int hsyncstart, hsyncend, htotal, hdispend;
  163. int vtotal, vdispend;
  164. int tmp;
  165. int sr07 = 0, hdr = 0;
  166. htotal = mode->htotal / 8;
  167. hsyncend = mode->hsync_end / 8;
  168. hsyncstart = mode->hsync_start / 8;
  169. hdispend = mode->hdisplay / 8;
  170. vtotal = mode->vtotal;
  171. vdispend = mode->vdisplay;
  172. vdispend -= 1;
  173. vtotal -= 2;
  174. htotal -= 5;
  175. hdispend -= 1;
  176. hsyncstart += 1;
  177. hsyncend += 1;
  178. WREG_CRT(VGA_CRTC_V_SYNC_END, 0x20);
  179. WREG_CRT(VGA_CRTC_H_TOTAL, htotal);
  180. WREG_CRT(VGA_CRTC_H_DISP, hdispend);
  181. WREG_CRT(VGA_CRTC_H_SYNC_START, hsyncstart);
  182. WREG_CRT(VGA_CRTC_H_SYNC_END, hsyncend);
  183. WREG_CRT(VGA_CRTC_V_TOTAL, vtotal & 0xff);
  184. WREG_CRT(VGA_CRTC_V_DISP_END, vdispend & 0xff);
  185. tmp = 0x40;
  186. if ((vdispend + 1) & 512)
  187. tmp |= 0x20;
  188. WREG_CRT(VGA_CRTC_MAX_SCAN, tmp);
  189. /*
  190. * Overflow bits for values that don't fit in the standard registers
  191. */
  192. tmp = 16;
  193. if (vtotal & 256)
  194. tmp |= 1;
  195. if (vdispend & 256)
  196. tmp |= 2;
  197. if ((vdispend + 1) & 256)
  198. tmp |= 8;
  199. if (vtotal & 512)
  200. tmp |= 32;
  201. if (vdispend & 512)
  202. tmp |= 64;
  203. WREG_CRT(VGA_CRTC_OVERFLOW, tmp);
  204. tmp = 0;
  205. /* More overflow bits */
  206. if ((htotal + 5) & 64)
  207. tmp |= 16;
  208. if ((htotal + 5) & 128)
  209. tmp |= 32;
  210. if (vtotal & 256)
  211. tmp |= 64;
  212. if (vtotal & 512)
  213. tmp |= 128;
  214. WREG_CRT(CL_CRT1A, tmp);
  215. /* Disable Hercules/CGA compatibility */
  216. WREG_CRT(VGA_CRTC_MODE, 0x03);
  217. WREG8(SEQ_INDEX, 0x7);
  218. sr07 = RREG8(SEQ_DATA);
  219. sr07 &= 0xe0;
  220. hdr = 0;
  221. switch (crtc->primary->fb->bits_per_pixel) {
  222. case 8:
  223. sr07 |= 0x11;
  224. break;
  225. case 16:
  226. sr07 |= 0x17;
  227. hdr = 0xc1;
  228. break;
  229. case 24:
  230. sr07 |= 0x15;
  231. hdr = 0xc5;
  232. break;
  233. case 32:
  234. sr07 |= 0x19;
  235. hdr = 0xc5;
  236. break;
  237. default:
  238. return -1;
  239. }
  240. WREG_SEQ(0x7, sr07);
  241. /* Program the pitch */
  242. tmp = crtc->primary->fb->pitches[0] / 8;
  243. WREG_CRT(VGA_CRTC_OFFSET, tmp);
  244. /* Enable extended blanking and pitch bits, and enable full memory */
  245. tmp = 0x22;
  246. tmp |= (crtc->primary->fb->pitches[0] >> 7) & 0x10;
  247. tmp |= (crtc->primary->fb->pitches[0] >> 6) & 0x40;
  248. WREG_CRT(0x1b, tmp);
  249. /* Enable high-colour modes */
  250. WREG_GFX(VGA_GFX_MODE, 0x40);
  251. /* And set graphics mode */
  252. WREG_GFX(VGA_GFX_MISC, 0x01);
  253. WREG_HDR(hdr);
  254. cirrus_crtc_do_set_base(crtc, old_fb, x, y, 0);
  255. /* Unblank (needed on S3 resume, vgabios doesn't do it then) */
  256. outb(0x20, 0x3c0);
  257. return 0;
  258. }
  259. /*
  260. * This is called before a mode is programmed. A typical use might be to
  261. * enable DPMS during the programming to avoid seeing intermediate stages,
  262. * but that's not relevant to us
  263. */
  264. static void cirrus_crtc_prepare(struct drm_crtc *crtc)
  265. {
  266. }
  267. /*
  268. * This is called after a mode is programmed. It should reverse anything done
  269. * by the prepare function
  270. */
  271. static void cirrus_crtc_commit(struct drm_crtc *crtc)
  272. {
  273. }
  274. /*
  275. * The core can pass us a set of gamma values to program. We actually only
  276. * use this for 8-bit mode so can't perform smooth fades on deeper modes,
  277. * but it's a requirement that we provide the function
  278. */
  279. static int cirrus_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  280. u16 *blue, uint32_t size)
  281. {
  282. struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
  283. int i;
  284. for (i = 0; i < size; i++) {
  285. cirrus_crtc->lut_r[i] = red[i];
  286. cirrus_crtc->lut_g[i] = green[i];
  287. cirrus_crtc->lut_b[i] = blue[i];
  288. }
  289. cirrus_crtc_load_lut(crtc);
  290. return 0;
  291. }
  292. /* Simple cleanup function */
  293. static void cirrus_crtc_destroy(struct drm_crtc *crtc)
  294. {
  295. struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
  296. drm_crtc_cleanup(crtc);
  297. kfree(cirrus_crtc);
  298. }
  299. /* These provide the minimum set of functions required to handle a CRTC */
  300. static const struct drm_crtc_funcs cirrus_crtc_funcs = {
  301. .gamma_set = cirrus_crtc_gamma_set,
  302. .set_config = drm_crtc_helper_set_config,
  303. .destroy = cirrus_crtc_destroy,
  304. };
  305. static const struct drm_crtc_helper_funcs cirrus_helper_funcs = {
  306. .dpms = cirrus_crtc_dpms,
  307. .mode_set = cirrus_crtc_mode_set,
  308. .mode_set_base = cirrus_crtc_mode_set_base,
  309. .prepare = cirrus_crtc_prepare,
  310. .commit = cirrus_crtc_commit,
  311. .load_lut = cirrus_crtc_load_lut,
  312. };
  313. /* CRTC setup */
  314. static void cirrus_crtc_init(struct drm_device *dev)
  315. {
  316. struct cirrus_device *cdev = dev->dev_private;
  317. struct cirrus_crtc *cirrus_crtc;
  318. int i;
  319. cirrus_crtc = kzalloc(sizeof(struct cirrus_crtc) +
  320. (CIRRUSFB_CONN_LIMIT * sizeof(struct drm_connector *)),
  321. GFP_KERNEL);
  322. if (cirrus_crtc == NULL)
  323. return;
  324. drm_crtc_init(dev, &cirrus_crtc->base, &cirrus_crtc_funcs);
  325. drm_mode_crtc_set_gamma_size(&cirrus_crtc->base, CIRRUS_LUT_SIZE);
  326. cdev->mode_info.crtc = cirrus_crtc;
  327. for (i = 0; i < CIRRUS_LUT_SIZE; i++) {
  328. cirrus_crtc->lut_r[i] = i;
  329. cirrus_crtc->lut_g[i] = i;
  330. cirrus_crtc->lut_b[i] = i;
  331. }
  332. drm_crtc_helper_add(&cirrus_crtc->base, &cirrus_helper_funcs);
  333. }
  334. /** Sets the color ramps on behalf of fbcon */
  335. void cirrus_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  336. u16 blue, int regno)
  337. {
  338. struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
  339. cirrus_crtc->lut_r[regno] = red;
  340. cirrus_crtc->lut_g[regno] = green;
  341. cirrus_crtc->lut_b[regno] = blue;
  342. }
  343. /** Gets the color ramps on behalf of fbcon */
  344. void cirrus_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  345. u16 *blue, int regno)
  346. {
  347. struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
  348. *red = cirrus_crtc->lut_r[regno];
  349. *green = cirrus_crtc->lut_g[regno];
  350. *blue = cirrus_crtc->lut_b[regno];
  351. }
  352. static void cirrus_encoder_mode_set(struct drm_encoder *encoder,
  353. struct drm_display_mode *mode,
  354. struct drm_display_mode *adjusted_mode)
  355. {
  356. }
  357. static void cirrus_encoder_dpms(struct drm_encoder *encoder, int state)
  358. {
  359. return;
  360. }
  361. static void cirrus_encoder_prepare(struct drm_encoder *encoder)
  362. {
  363. }
  364. static void cirrus_encoder_commit(struct drm_encoder *encoder)
  365. {
  366. }
  367. static void cirrus_encoder_destroy(struct drm_encoder *encoder)
  368. {
  369. struct cirrus_encoder *cirrus_encoder = to_cirrus_encoder(encoder);
  370. drm_encoder_cleanup(encoder);
  371. kfree(cirrus_encoder);
  372. }
  373. static const struct drm_encoder_helper_funcs cirrus_encoder_helper_funcs = {
  374. .dpms = cirrus_encoder_dpms,
  375. .mode_set = cirrus_encoder_mode_set,
  376. .prepare = cirrus_encoder_prepare,
  377. .commit = cirrus_encoder_commit,
  378. };
  379. static const struct drm_encoder_funcs cirrus_encoder_encoder_funcs = {
  380. .destroy = cirrus_encoder_destroy,
  381. };
  382. static struct drm_encoder *cirrus_encoder_init(struct drm_device *dev)
  383. {
  384. struct drm_encoder *encoder;
  385. struct cirrus_encoder *cirrus_encoder;
  386. cirrus_encoder = kzalloc(sizeof(struct cirrus_encoder), GFP_KERNEL);
  387. if (!cirrus_encoder)
  388. return NULL;
  389. encoder = &cirrus_encoder->base;
  390. encoder->possible_crtcs = 0x1;
  391. drm_encoder_init(dev, encoder, &cirrus_encoder_encoder_funcs,
  392. DRM_MODE_ENCODER_DAC, NULL);
  393. drm_encoder_helper_add(encoder, &cirrus_encoder_helper_funcs);
  394. return encoder;
  395. }
  396. static int cirrus_vga_get_modes(struct drm_connector *connector)
  397. {
  398. int count;
  399. /* Just add a static list of modes */
  400. if (cirrus_bpp <= 24) {
  401. count = drm_add_modes_noedid(connector, 1280, 1024);
  402. drm_set_preferred_mode(connector, 1024, 768);
  403. } else {
  404. count = drm_add_modes_noedid(connector, 800, 600);
  405. drm_set_preferred_mode(connector, 800, 600);
  406. }
  407. return count;
  408. }
  409. static struct drm_encoder *cirrus_connector_best_encoder(struct drm_connector
  410. *connector)
  411. {
  412. int enc_id = connector->encoder_ids[0];
  413. /* pick the encoder ids */
  414. if (enc_id)
  415. return drm_encoder_find(connector->dev, enc_id);
  416. return NULL;
  417. }
  418. static enum drm_connector_status cirrus_vga_detect(struct drm_connector
  419. *connector, bool force)
  420. {
  421. return connector_status_connected;
  422. }
  423. static void cirrus_connector_destroy(struct drm_connector *connector)
  424. {
  425. drm_connector_cleanup(connector);
  426. kfree(connector);
  427. }
  428. static const struct drm_connector_helper_funcs cirrus_vga_connector_helper_funcs = {
  429. .get_modes = cirrus_vga_get_modes,
  430. .best_encoder = cirrus_connector_best_encoder,
  431. };
  432. static const struct drm_connector_funcs cirrus_vga_connector_funcs = {
  433. .dpms = drm_helper_connector_dpms,
  434. .detect = cirrus_vga_detect,
  435. .fill_modes = drm_helper_probe_single_connector_modes,
  436. .destroy = cirrus_connector_destroy,
  437. };
  438. static struct drm_connector *cirrus_vga_init(struct drm_device *dev)
  439. {
  440. struct drm_connector *connector;
  441. struct cirrus_connector *cirrus_connector;
  442. cirrus_connector = kzalloc(sizeof(struct cirrus_connector), GFP_KERNEL);
  443. if (!cirrus_connector)
  444. return NULL;
  445. connector = &cirrus_connector->base;
  446. drm_connector_init(dev, connector,
  447. &cirrus_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  448. drm_connector_helper_add(connector, &cirrus_vga_connector_helper_funcs);
  449. drm_connector_register(connector);
  450. return connector;
  451. }
  452. int cirrus_modeset_init(struct cirrus_device *cdev)
  453. {
  454. struct drm_encoder *encoder;
  455. struct drm_connector *connector;
  456. int ret;
  457. drm_mode_config_init(cdev->dev);
  458. cdev->mode_info.mode_config_initialized = true;
  459. cdev->dev->mode_config.max_width = CIRRUS_MAX_FB_WIDTH;
  460. cdev->dev->mode_config.max_height = CIRRUS_MAX_FB_HEIGHT;
  461. cdev->dev->mode_config.fb_base = cdev->mc.vram_base;
  462. cdev->dev->mode_config.preferred_depth = 24;
  463. /* don't prefer a shadow on virt GPU */
  464. cdev->dev->mode_config.prefer_shadow = 0;
  465. cirrus_crtc_init(cdev->dev);
  466. encoder = cirrus_encoder_init(cdev->dev);
  467. if (!encoder) {
  468. DRM_ERROR("cirrus_encoder_init failed\n");
  469. return -1;
  470. }
  471. connector = cirrus_vga_init(cdev->dev);
  472. if (!connector) {
  473. DRM_ERROR("cirrus_vga_init failed\n");
  474. return -1;
  475. }
  476. drm_mode_connector_attach_encoder(connector, encoder);
  477. ret = cirrus_fbdev_init(cdev);
  478. if (ret) {
  479. DRM_ERROR("cirrus_fbdev_init failed\n");
  480. return ret;
  481. }
  482. return 0;
  483. }
  484. void cirrus_modeset_fini(struct cirrus_device *cdev)
  485. {
  486. cirrus_fbdev_fini(cdev);
  487. if (cdev->mode_info.mode_config_initialized) {
  488. drm_mode_config_cleanup(cdev->dev);
  489. cdev->mode_info.mode_config_initialized = false;
  490. }
  491. }