ast_main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sub license, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  15. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  16. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  17. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  18. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. *
  20. * The above copyright notice and this permission notice (including the
  21. * next paragraph) shall be included in all copies or substantial portions
  22. * of the Software.
  23. *
  24. */
  25. /*
  26. * Authors: Dave Airlie <airlied@redhat.com>
  27. */
  28. #include <drm/drmP.h>
  29. #include "ast_drv.h"
  30. #include <drm/drm_fb_helper.h>
  31. #include <drm/drm_crtc_helper.h>
  32. #include "ast_dram_tables.h"
  33. void ast_set_index_reg_mask(struct ast_private *ast,
  34. uint32_t base, uint8_t index,
  35. uint8_t mask, uint8_t val)
  36. {
  37. u8 tmp;
  38. ast_io_write8(ast, base, index);
  39. tmp = (ast_io_read8(ast, base + 1) & mask) | val;
  40. ast_set_index_reg(ast, base, index, tmp);
  41. }
  42. uint8_t ast_get_index_reg(struct ast_private *ast,
  43. uint32_t base, uint8_t index)
  44. {
  45. uint8_t ret;
  46. ast_io_write8(ast, base, index);
  47. ret = ast_io_read8(ast, base + 1);
  48. return ret;
  49. }
  50. uint8_t ast_get_index_reg_mask(struct ast_private *ast,
  51. uint32_t base, uint8_t index, uint8_t mask)
  52. {
  53. uint8_t ret;
  54. ast_io_write8(ast, base, index);
  55. ret = ast_io_read8(ast, base + 1) & mask;
  56. return ret;
  57. }
  58. static int ast_detect_chip(struct drm_device *dev, bool *need_post)
  59. {
  60. struct ast_private *ast = dev->dev_private;
  61. uint32_t data, jreg;
  62. if (dev->pdev->device == PCI_CHIP_AST1180) {
  63. ast->chip = AST1100;
  64. DRM_INFO("AST 1180 detected\n");
  65. } else {
  66. if (dev->pdev->revision >= 0x30) {
  67. ast->chip = AST2400;
  68. DRM_INFO("AST 2400 detected\n");
  69. } else if (dev->pdev->revision >= 0x20) {
  70. ast->chip = AST2300;
  71. DRM_INFO("AST 2300 detected\n");
  72. } else if (dev->pdev->revision >= 0x10) {
  73. uint32_t data;
  74. ast_write32(ast, 0xf004, 0x1e6e0000);
  75. ast_write32(ast, 0xf000, 0x1);
  76. data = ast_read32(ast, 0x1207c);
  77. switch (data & 0x0300) {
  78. case 0x0200:
  79. ast->chip = AST1100;
  80. DRM_INFO("AST 1100 detected\n");
  81. break;
  82. case 0x0100:
  83. ast->chip = AST2200;
  84. DRM_INFO("AST 2200 detected\n");
  85. break;
  86. case 0x0000:
  87. ast->chip = AST2150;
  88. DRM_INFO("AST 2150 detected\n");
  89. break;
  90. default:
  91. ast->chip = AST2100;
  92. DRM_INFO("AST 2100 detected\n");
  93. break;
  94. }
  95. ast->vga2_clone = false;
  96. } else {
  97. ast->chip = 2000;
  98. DRM_INFO("AST 2000 detected\n");
  99. }
  100. }
  101. /*
  102. * If VGA isn't enabled, we need to enable now or subsequent
  103. * access to the scratch registers will fail. We also inform
  104. * our caller that it needs to POST the chip
  105. * (Assumption: VGA not enabled -> need to POST)
  106. */
  107. if (!ast_is_vga_enabled(dev)) {
  108. ast_enable_vga(dev);
  109. ast_enable_mmio(dev);
  110. DRM_INFO("VGA not enabled on entry, requesting chip POST\n");
  111. *need_post = true;
  112. } else
  113. *need_post = false;
  114. /* Check if we support wide screen */
  115. switch (ast->chip) {
  116. case AST1180:
  117. ast->support_wide_screen = true;
  118. break;
  119. case AST2000:
  120. ast->support_wide_screen = false;
  121. break;
  122. default:
  123. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
  124. if (!(jreg & 0x80))
  125. ast->support_wide_screen = true;
  126. else if (jreg & 0x01)
  127. ast->support_wide_screen = true;
  128. else {
  129. ast->support_wide_screen = false;
  130. /* Read SCU7c (silicon revision register) */
  131. ast_write32(ast, 0xf004, 0x1e6e0000);
  132. ast_write32(ast, 0xf000, 0x1);
  133. data = ast_read32(ast, 0x1207c);
  134. data &= 0x300;
  135. if (ast->chip == AST2300 && data == 0x0) /* ast1300 */
  136. ast->support_wide_screen = true;
  137. if (ast->chip == AST2400 && data == 0x100) /* ast1400 */
  138. ast->support_wide_screen = true;
  139. }
  140. break;
  141. }
  142. /* Check 3rd Tx option (digital output afaik) */
  143. ast->tx_chip_type = AST_TX_NONE;
  144. /*
  145. * VGACRA3 Enhanced Color Mode Register, check if DVO is already
  146. * enabled, in that case, assume we have a SIL164 TMDS transmitter
  147. */
  148. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xff);
  149. if (jreg & 0x80)
  150. ast->tx_chip_type = AST_TX_SIL164;
  151. if ((ast->chip == AST2300) || (ast->chip == AST2400)) {
  152. /*
  153. * On AST2300 and 2400, look the configuration set by the SoC in
  154. * the SOC scratch register #1 bits 11:8 (interestingly marked
  155. * as "reserved" in the spec
  156. */
  157. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
  158. switch (jreg) {
  159. case 0x04:
  160. ast->tx_chip_type = AST_TX_SIL164;
  161. break;
  162. case 0x08:
  163. ast->dp501_fw_addr = kzalloc(32*1024, GFP_KERNEL);
  164. if (ast->dp501_fw_addr) {
  165. /* backup firmware */
  166. if (ast_backup_fw(dev, ast->dp501_fw_addr, 32*1024)) {
  167. kfree(ast->dp501_fw_addr);
  168. ast->dp501_fw_addr = NULL;
  169. }
  170. }
  171. /* fallthrough */
  172. case 0x0c:
  173. ast->tx_chip_type = AST_TX_DP501;
  174. }
  175. }
  176. /* Print stuff for diagnostic purposes */
  177. switch(ast->tx_chip_type) {
  178. case AST_TX_SIL164:
  179. DRM_INFO("Using Sil164 TMDS transmitter\n");
  180. break;
  181. case AST_TX_DP501:
  182. DRM_INFO("Using DP501 DisplayPort transmitter\n");
  183. break;
  184. default:
  185. DRM_INFO("Analog VGA only\n");
  186. }
  187. return 0;
  188. }
  189. static int ast_get_dram_info(struct drm_device *dev)
  190. {
  191. struct ast_private *ast = dev->dev_private;
  192. uint32_t data, data2;
  193. uint32_t denum, num, div, ref_pll;
  194. ast_write32(ast, 0xf004, 0x1e6e0000);
  195. ast_write32(ast, 0xf000, 0x1);
  196. ast_write32(ast, 0x10000, 0xfc600309);
  197. do {
  198. ;
  199. } while (ast_read32(ast, 0x10000) != 0x01);
  200. data = ast_read32(ast, 0x10004);
  201. if (data & 0x400)
  202. ast->dram_bus_width = 16;
  203. else
  204. ast->dram_bus_width = 32;
  205. if (ast->chip == AST2300 || ast->chip == AST2400) {
  206. switch (data & 0x03) {
  207. case 0:
  208. ast->dram_type = AST_DRAM_512Mx16;
  209. break;
  210. default:
  211. case 1:
  212. ast->dram_type = AST_DRAM_1Gx16;
  213. break;
  214. case 2:
  215. ast->dram_type = AST_DRAM_2Gx16;
  216. break;
  217. case 3:
  218. ast->dram_type = AST_DRAM_4Gx16;
  219. break;
  220. }
  221. } else {
  222. switch (data & 0x0c) {
  223. case 0:
  224. case 4:
  225. ast->dram_type = AST_DRAM_512Mx16;
  226. break;
  227. case 8:
  228. if (data & 0x40)
  229. ast->dram_type = AST_DRAM_1Gx16;
  230. else
  231. ast->dram_type = AST_DRAM_512Mx32;
  232. break;
  233. case 0xc:
  234. ast->dram_type = AST_DRAM_1Gx32;
  235. break;
  236. }
  237. }
  238. data = ast_read32(ast, 0x10120);
  239. data2 = ast_read32(ast, 0x10170);
  240. if (data2 & 0x2000)
  241. ref_pll = 14318;
  242. else
  243. ref_pll = 12000;
  244. denum = data & 0x1f;
  245. num = (data & 0x3fe0) >> 5;
  246. data = (data & 0xc000) >> 14;
  247. switch (data) {
  248. case 3:
  249. div = 0x4;
  250. break;
  251. case 2:
  252. case 1:
  253. div = 0x2;
  254. break;
  255. default:
  256. div = 0x1;
  257. break;
  258. }
  259. ast->mclk = ref_pll * (num + 2) / (denum + 2) * (div * 1000);
  260. return 0;
  261. }
  262. static void ast_user_framebuffer_destroy(struct drm_framebuffer *fb)
  263. {
  264. struct ast_framebuffer *ast_fb = to_ast_framebuffer(fb);
  265. if (ast_fb->obj)
  266. drm_gem_object_unreference_unlocked(ast_fb->obj);
  267. drm_framebuffer_cleanup(fb);
  268. kfree(fb);
  269. }
  270. static const struct drm_framebuffer_funcs ast_fb_funcs = {
  271. .destroy = ast_user_framebuffer_destroy,
  272. };
  273. int ast_framebuffer_init(struct drm_device *dev,
  274. struct ast_framebuffer *ast_fb,
  275. struct drm_mode_fb_cmd2 *mode_cmd,
  276. struct drm_gem_object *obj)
  277. {
  278. int ret;
  279. drm_helper_mode_fill_fb_struct(&ast_fb->base, mode_cmd);
  280. ast_fb->obj = obj;
  281. ret = drm_framebuffer_init(dev, &ast_fb->base, &ast_fb_funcs);
  282. if (ret) {
  283. DRM_ERROR("framebuffer init failed %d\n", ret);
  284. return ret;
  285. }
  286. return 0;
  287. }
  288. static struct drm_framebuffer *
  289. ast_user_framebuffer_create(struct drm_device *dev,
  290. struct drm_file *filp,
  291. struct drm_mode_fb_cmd2 *mode_cmd)
  292. {
  293. struct drm_gem_object *obj;
  294. struct ast_framebuffer *ast_fb;
  295. int ret;
  296. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
  297. if (obj == NULL)
  298. return ERR_PTR(-ENOENT);
  299. ast_fb = kzalloc(sizeof(*ast_fb), GFP_KERNEL);
  300. if (!ast_fb) {
  301. drm_gem_object_unreference_unlocked(obj);
  302. return ERR_PTR(-ENOMEM);
  303. }
  304. ret = ast_framebuffer_init(dev, ast_fb, mode_cmd, obj);
  305. if (ret) {
  306. drm_gem_object_unreference_unlocked(obj);
  307. kfree(ast_fb);
  308. return ERR_PTR(ret);
  309. }
  310. return &ast_fb->base;
  311. }
  312. static const struct drm_mode_config_funcs ast_mode_funcs = {
  313. .fb_create = ast_user_framebuffer_create,
  314. };
  315. static u32 ast_get_vram_info(struct drm_device *dev)
  316. {
  317. struct ast_private *ast = dev->dev_private;
  318. u8 jreg;
  319. u32 vram_size;
  320. ast_open_key(ast);
  321. vram_size = AST_VIDMEM_DEFAULT_SIZE;
  322. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xaa, 0xff);
  323. switch (jreg & 3) {
  324. case 0: vram_size = AST_VIDMEM_SIZE_8M; break;
  325. case 1: vram_size = AST_VIDMEM_SIZE_16M; break;
  326. case 2: vram_size = AST_VIDMEM_SIZE_32M; break;
  327. case 3: vram_size = AST_VIDMEM_SIZE_64M; break;
  328. }
  329. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x99, 0xff);
  330. switch (jreg & 0x03) {
  331. case 1:
  332. vram_size -= 0x100000;
  333. break;
  334. case 2:
  335. vram_size -= 0x200000;
  336. break;
  337. case 3:
  338. vram_size -= 0x400000;
  339. break;
  340. }
  341. return vram_size;
  342. }
  343. int ast_driver_load(struct drm_device *dev, unsigned long flags)
  344. {
  345. struct ast_private *ast;
  346. bool need_post;
  347. int ret = 0;
  348. ast = kzalloc(sizeof(struct ast_private), GFP_KERNEL);
  349. if (!ast)
  350. return -ENOMEM;
  351. dev->dev_private = ast;
  352. ast->dev = dev;
  353. ast->regs = pci_iomap(dev->pdev, 1, 0);
  354. if (!ast->regs) {
  355. ret = -EIO;
  356. goto out_free;
  357. }
  358. /*
  359. * If we don't have IO space at all, use MMIO now and
  360. * assume the chip has MMIO enabled by default (rev 0x20
  361. * and higher).
  362. */
  363. if (!(pci_resource_flags(dev->pdev, 2) & IORESOURCE_IO)) {
  364. DRM_INFO("platform has no IO space, trying MMIO\n");
  365. ast->ioregs = ast->regs + AST_IO_MM_OFFSET;
  366. }
  367. /* "map" IO regs if the above hasn't done so already */
  368. if (!ast->ioregs) {
  369. ast->ioregs = pci_iomap(dev->pdev, 2, 0);
  370. if (!ast->ioregs) {
  371. ret = -EIO;
  372. goto out_free;
  373. }
  374. }
  375. ast_detect_chip(dev, &need_post);
  376. if (ast->chip != AST1180) {
  377. ast_get_dram_info(dev);
  378. ast->vram_size = ast_get_vram_info(dev);
  379. DRM_INFO("dram %d %d %d %08x\n", ast->mclk, ast->dram_type, ast->dram_bus_width, ast->vram_size);
  380. }
  381. if (need_post)
  382. ast_post_gpu(dev);
  383. ret = ast_mm_init(ast);
  384. if (ret)
  385. goto out_free;
  386. drm_mode_config_init(dev);
  387. dev->mode_config.funcs = (void *)&ast_mode_funcs;
  388. dev->mode_config.min_width = 0;
  389. dev->mode_config.min_height = 0;
  390. dev->mode_config.preferred_depth = 24;
  391. dev->mode_config.prefer_shadow = 1;
  392. if (ast->chip == AST2100 ||
  393. ast->chip == AST2200 ||
  394. ast->chip == AST2300 ||
  395. ast->chip == AST2400 ||
  396. ast->chip == AST1180) {
  397. dev->mode_config.max_width = 1920;
  398. dev->mode_config.max_height = 2048;
  399. } else {
  400. dev->mode_config.max_width = 1600;
  401. dev->mode_config.max_height = 1200;
  402. }
  403. ret = ast_mode_init(dev);
  404. if (ret)
  405. goto out_free;
  406. ret = ast_fbdev_init(dev);
  407. if (ret)
  408. goto out_free;
  409. return 0;
  410. out_free:
  411. kfree(ast);
  412. dev->dev_private = NULL;
  413. return ret;
  414. }
  415. int ast_driver_unload(struct drm_device *dev)
  416. {
  417. struct ast_private *ast = dev->dev_private;
  418. kfree(ast->dp501_fw_addr);
  419. ast_mode_fini(dev);
  420. ast_fbdev_fini(dev);
  421. drm_mode_config_cleanup(dev);
  422. ast_mm_fini(ast);
  423. pci_iounmap(dev->pdev, ast->ioregs);
  424. pci_iounmap(dev->pdev, ast->regs);
  425. kfree(ast);
  426. return 0;
  427. }
  428. int ast_gem_create(struct drm_device *dev,
  429. u32 size, bool iskernel,
  430. struct drm_gem_object **obj)
  431. {
  432. struct ast_bo *astbo;
  433. int ret;
  434. *obj = NULL;
  435. size = roundup(size, PAGE_SIZE);
  436. if (size == 0)
  437. return -EINVAL;
  438. ret = ast_bo_create(dev, size, 0, 0, &astbo);
  439. if (ret) {
  440. if (ret != -ERESTARTSYS)
  441. DRM_ERROR("failed to allocate GEM object\n");
  442. return ret;
  443. }
  444. *obj = &astbo->gem;
  445. return 0;
  446. }
  447. int ast_dumb_create(struct drm_file *file,
  448. struct drm_device *dev,
  449. struct drm_mode_create_dumb *args)
  450. {
  451. int ret;
  452. struct drm_gem_object *gobj;
  453. u32 handle;
  454. args->pitch = args->width * ((args->bpp + 7) / 8);
  455. args->size = args->pitch * args->height;
  456. ret = ast_gem_create(dev, args->size, false,
  457. &gobj);
  458. if (ret)
  459. return ret;
  460. ret = drm_gem_handle_create(file, gobj, &handle);
  461. drm_gem_object_unreference_unlocked(gobj);
  462. if (ret)
  463. return ret;
  464. args->handle = handle;
  465. return 0;
  466. }
  467. static void ast_bo_unref(struct ast_bo **bo)
  468. {
  469. struct ttm_buffer_object *tbo;
  470. if ((*bo) == NULL)
  471. return;
  472. tbo = &((*bo)->bo);
  473. ttm_bo_unref(&tbo);
  474. *bo = NULL;
  475. }
  476. void ast_gem_free_object(struct drm_gem_object *obj)
  477. {
  478. struct ast_bo *ast_bo = gem_to_ast_bo(obj);
  479. ast_bo_unref(&ast_bo);
  480. }
  481. static inline u64 ast_bo_mmap_offset(struct ast_bo *bo)
  482. {
  483. return drm_vma_node_offset_addr(&bo->bo.vma_node);
  484. }
  485. int
  486. ast_dumb_mmap_offset(struct drm_file *file,
  487. struct drm_device *dev,
  488. uint32_t handle,
  489. uint64_t *offset)
  490. {
  491. struct drm_gem_object *obj;
  492. int ret;
  493. struct ast_bo *bo;
  494. mutex_lock(&dev->struct_mutex);
  495. obj = drm_gem_object_lookup(dev, file, handle);
  496. if (obj == NULL) {
  497. ret = -ENOENT;
  498. goto out_unlock;
  499. }
  500. bo = gem_to_ast_bo(obj);
  501. *offset = ast_bo_mmap_offset(bo);
  502. drm_gem_object_unreference(obj);
  503. ret = 0;
  504. out_unlock:
  505. mutex_unlock(&dev->struct_mutex);
  506. return ret;
  507. }