ast_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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)
  59. {
  60. struct ast_private *ast = dev->dev_private;
  61. if (dev->pdev->device == PCI_CHIP_AST1180) {
  62. ast->chip = AST1100;
  63. DRM_INFO("AST 1180 detected\n");
  64. } else {
  65. if (dev->pdev->revision >= 0x20) {
  66. ast->chip = AST2300;
  67. DRM_INFO("AST 2300 detected\n");
  68. } else if (dev->pdev->revision >= 0x10) {
  69. uint32_t data;
  70. ast_write32(ast, 0xf004, 0x1e6e0000);
  71. ast_write32(ast, 0xf000, 0x1);
  72. data = ast_read32(ast, 0x1207c);
  73. switch (data & 0x0300) {
  74. case 0x0200:
  75. ast->chip = AST1100;
  76. DRM_INFO("AST 1100 detected\n");
  77. break;
  78. case 0x0100:
  79. ast->chip = AST2200;
  80. DRM_INFO("AST 2200 detected\n");
  81. break;
  82. case 0x0000:
  83. ast->chip = AST2150;
  84. DRM_INFO("AST 2150 detected\n");
  85. break;
  86. default:
  87. ast->chip = AST2100;
  88. DRM_INFO("AST 2100 detected\n");
  89. break;
  90. }
  91. ast->vga2_clone = false;
  92. } else {
  93. ast->chip = 2000;
  94. DRM_INFO("AST 2000 detected\n");
  95. }
  96. }
  97. return 0;
  98. }
  99. static int ast_get_dram_info(struct drm_device *dev)
  100. {
  101. struct ast_private *ast = dev->dev_private;
  102. uint32_t data, data2;
  103. uint32_t denum, num, div, ref_pll;
  104. ast_write32(ast, 0xf004, 0x1e6e0000);
  105. ast_write32(ast, 0xf000, 0x1);
  106. ast_write32(ast, 0x10000, 0xfc600309);
  107. do {
  108. ;
  109. } while (ast_read32(ast, 0x10000) != 0x01);
  110. data = ast_read32(ast, 0x10004);
  111. if (data & 0x400)
  112. ast->dram_bus_width = 16;
  113. else
  114. ast->dram_bus_width = 32;
  115. if (ast->chip == AST2300) {
  116. switch (data & 0x03) {
  117. case 0:
  118. ast->dram_type = AST_DRAM_512Mx16;
  119. break;
  120. default:
  121. case 1:
  122. ast->dram_type = AST_DRAM_1Gx16;
  123. break;
  124. case 2:
  125. ast->dram_type = AST_DRAM_2Gx16;
  126. break;
  127. case 3:
  128. ast->dram_type = AST_DRAM_4Gx16;
  129. break;
  130. }
  131. } else {
  132. switch (data & 0x0c) {
  133. case 0:
  134. case 4:
  135. ast->dram_type = AST_DRAM_512Mx16;
  136. break;
  137. case 8:
  138. if (data & 0x40)
  139. ast->dram_type = AST_DRAM_1Gx16;
  140. else
  141. ast->dram_type = AST_DRAM_512Mx32;
  142. break;
  143. case 0xc:
  144. ast->dram_type = AST_DRAM_1Gx32;
  145. break;
  146. }
  147. }
  148. data = ast_read32(ast, 0x10120);
  149. data2 = ast_read32(ast, 0x10170);
  150. if (data2 & 0x2000)
  151. ref_pll = 14318;
  152. else
  153. ref_pll = 12000;
  154. denum = data & 0x1f;
  155. num = (data & 0x3fe0) >> 5;
  156. data = (data & 0xc000) >> 14;
  157. switch (data) {
  158. case 3:
  159. div = 0x4;
  160. break;
  161. case 2:
  162. case 1:
  163. div = 0x2;
  164. break;
  165. default:
  166. div = 0x1;
  167. break;
  168. }
  169. ast->mclk = ref_pll * (num + 2) / (denum + 2) * (div * 1000);
  170. return 0;
  171. }
  172. uint32_t ast_get_max_dclk(struct drm_device *dev, int bpp)
  173. {
  174. struct ast_private *ast = dev->dev_private;
  175. uint32_t dclk, jreg;
  176. uint32_t dram_bus_width, mclk, dram_bandwidth, actual_dram_bandwidth, dram_efficency = 500;
  177. dram_bus_width = ast->dram_bus_width;
  178. mclk = ast->mclk;
  179. if (ast->chip == AST2100 ||
  180. ast->chip == AST1100 ||
  181. ast->chip == AST2200 ||
  182. ast->chip == AST2150 ||
  183. ast->dram_bus_width == 16)
  184. dram_efficency = 600;
  185. else if (ast->chip == AST2300)
  186. dram_efficency = 400;
  187. dram_bandwidth = mclk * dram_bus_width * 2 / 8;
  188. actual_dram_bandwidth = dram_bandwidth * dram_efficency / 1000;
  189. if (ast->chip == AST1180)
  190. dclk = actual_dram_bandwidth / ((bpp + 1) / 8);
  191. else {
  192. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
  193. if ((jreg & 0x08) && (ast->chip == AST2000))
  194. dclk = actual_dram_bandwidth / ((bpp + 1 + 16) / 8);
  195. else if ((jreg & 0x08) && (bpp == 8))
  196. dclk = actual_dram_bandwidth / ((bpp + 1 + 24) / 8);
  197. else
  198. dclk = actual_dram_bandwidth / ((bpp + 1) / 8);
  199. }
  200. if (ast->chip == AST2100 ||
  201. ast->chip == AST2200 ||
  202. ast->chip == AST2300 ||
  203. ast->chip == AST1180) {
  204. if (dclk > 200)
  205. dclk = 200;
  206. } else {
  207. if (dclk > 165)
  208. dclk = 165;
  209. }
  210. return dclk;
  211. }
  212. static void ast_user_framebuffer_destroy(struct drm_framebuffer *fb)
  213. {
  214. struct ast_framebuffer *ast_fb = to_ast_framebuffer(fb);
  215. if (ast_fb->obj)
  216. drm_gem_object_unreference_unlocked(ast_fb->obj);
  217. drm_framebuffer_cleanup(fb);
  218. kfree(fb);
  219. }
  220. static const struct drm_framebuffer_funcs ast_fb_funcs = {
  221. .destroy = ast_user_framebuffer_destroy,
  222. };
  223. int ast_framebuffer_init(struct drm_device *dev,
  224. struct ast_framebuffer *ast_fb,
  225. struct drm_mode_fb_cmd2 *mode_cmd,
  226. struct drm_gem_object *obj)
  227. {
  228. int ret;
  229. drm_helper_mode_fill_fb_struct(&ast_fb->base, mode_cmd);
  230. ast_fb->obj = obj;
  231. ret = drm_framebuffer_init(dev, &ast_fb->base, &ast_fb_funcs);
  232. if (ret) {
  233. DRM_ERROR("framebuffer init failed %d\n", ret);
  234. return ret;
  235. }
  236. return 0;
  237. }
  238. static struct drm_framebuffer *
  239. ast_user_framebuffer_create(struct drm_device *dev,
  240. struct drm_file *filp,
  241. struct drm_mode_fb_cmd2 *mode_cmd)
  242. {
  243. struct drm_gem_object *obj;
  244. struct ast_framebuffer *ast_fb;
  245. int ret;
  246. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
  247. if (obj == NULL)
  248. return ERR_PTR(-ENOENT);
  249. ast_fb = kzalloc(sizeof(*ast_fb), GFP_KERNEL);
  250. if (!ast_fb) {
  251. drm_gem_object_unreference_unlocked(obj);
  252. return ERR_PTR(-ENOMEM);
  253. }
  254. ret = ast_framebuffer_init(dev, ast_fb, mode_cmd, obj);
  255. if (ret) {
  256. drm_gem_object_unreference_unlocked(obj);
  257. kfree(ast_fb);
  258. return ERR_PTR(ret);
  259. }
  260. return &ast_fb->base;
  261. }
  262. static const struct drm_mode_config_funcs ast_mode_funcs = {
  263. .fb_create = ast_user_framebuffer_create,
  264. };
  265. static u32 ast_get_vram_info(struct drm_device *dev)
  266. {
  267. struct ast_private *ast = dev->dev_private;
  268. u8 jreg;
  269. ast_open_key(ast);
  270. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xaa, 0xff);
  271. switch (jreg & 3) {
  272. case 0: return AST_VIDMEM_SIZE_8M;
  273. case 1: return AST_VIDMEM_SIZE_16M;
  274. case 2: return AST_VIDMEM_SIZE_32M;
  275. case 3: return AST_VIDMEM_SIZE_64M;
  276. }
  277. return AST_VIDMEM_DEFAULT_SIZE;
  278. }
  279. int ast_driver_load(struct drm_device *dev, unsigned long flags)
  280. {
  281. struct ast_private *ast;
  282. int ret = 0;
  283. ast = kzalloc(sizeof(struct ast_private), GFP_KERNEL);
  284. if (!ast)
  285. return -ENOMEM;
  286. dev->dev_private = ast;
  287. ast->dev = dev;
  288. ast->regs = pci_iomap(dev->pdev, 1, 0);
  289. if (!ast->regs) {
  290. ret = -EIO;
  291. goto out_free;
  292. }
  293. ast->ioregs = pci_iomap(dev->pdev, 2, 0);
  294. if (!ast->ioregs) {
  295. ret = -EIO;
  296. goto out_free;
  297. }
  298. ast_detect_chip(dev);
  299. if (ast->chip != AST1180) {
  300. ast_get_dram_info(dev);
  301. ast->vram_size = ast_get_vram_info(dev);
  302. DRM_INFO("dram %d %d %d %08x\n", ast->mclk, ast->dram_type, ast->dram_bus_width, ast->vram_size);
  303. }
  304. ret = ast_mm_init(ast);
  305. if (ret)
  306. goto out_free;
  307. drm_mode_config_init(dev);
  308. dev->mode_config.funcs = (void *)&ast_mode_funcs;
  309. dev->mode_config.min_width = 0;
  310. dev->mode_config.min_height = 0;
  311. dev->mode_config.preferred_depth = 24;
  312. dev->mode_config.prefer_shadow = 1;
  313. if (ast->chip == AST2100 ||
  314. ast->chip == AST2200 ||
  315. ast->chip == AST2300 ||
  316. ast->chip == AST1180) {
  317. dev->mode_config.max_width = 1920;
  318. dev->mode_config.max_height = 2048;
  319. } else {
  320. dev->mode_config.max_width = 1600;
  321. dev->mode_config.max_height = 1200;
  322. }
  323. ret = ast_mode_init(dev);
  324. if (ret)
  325. goto out_free;
  326. ret = ast_fbdev_init(dev);
  327. if (ret)
  328. goto out_free;
  329. return 0;
  330. out_free:
  331. kfree(ast);
  332. dev->dev_private = NULL;
  333. return ret;
  334. }
  335. int ast_driver_unload(struct drm_device *dev)
  336. {
  337. struct ast_private *ast = dev->dev_private;
  338. ast_mode_fini(dev);
  339. ast_fbdev_fini(dev);
  340. drm_mode_config_cleanup(dev);
  341. ast_mm_fini(ast);
  342. pci_iounmap(dev->pdev, ast->ioregs);
  343. pci_iounmap(dev->pdev, ast->regs);
  344. kfree(ast);
  345. return 0;
  346. }
  347. int ast_gem_create(struct drm_device *dev,
  348. u32 size, bool iskernel,
  349. struct drm_gem_object **obj)
  350. {
  351. struct ast_bo *astbo;
  352. int ret;
  353. *obj = NULL;
  354. size = roundup(size, PAGE_SIZE);
  355. if (size == 0)
  356. return -EINVAL;
  357. ret = ast_bo_create(dev, size, 0, 0, &astbo);
  358. if (ret) {
  359. if (ret != -ERESTARTSYS)
  360. DRM_ERROR("failed to allocate GEM object\n");
  361. return ret;
  362. }
  363. *obj = &astbo->gem;
  364. return 0;
  365. }
  366. int ast_dumb_create(struct drm_file *file,
  367. struct drm_device *dev,
  368. struct drm_mode_create_dumb *args)
  369. {
  370. int ret;
  371. struct drm_gem_object *gobj;
  372. u32 handle;
  373. args->pitch = args->width * ((args->bpp + 7) / 8);
  374. args->size = args->pitch * args->height;
  375. ret = ast_gem_create(dev, args->size, false,
  376. &gobj);
  377. if (ret)
  378. return ret;
  379. ret = drm_gem_handle_create(file, gobj, &handle);
  380. drm_gem_object_unreference_unlocked(gobj);
  381. if (ret)
  382. return ret;
  383. args->handle = handle;
  384. return 0;
  385. }
  386. int ast_dumb_destroy(struct drm_file *file,
  387. struct drm_device *dev,
  388. uint32_t handle)
  389. {
  390. return drm_gem_handle_delete(file, handle);
  391. }
  392. int ast_gem_init_object(struct drm_gem_object *obj)
  393. {
  394. BUG();
  395. return 0;
  396. }
  397. void ast_bo_unref(struct ast_bo **bo)
  398. {
  399. struct ttm_buffer_object *tbo;
  400. if ((*bo) == NULL)
  401. return;
  402. tbo = &((*bo)->bo);
  403. ttm_bo_unref(&tbo);
  404. if (tbo == NULL)
  405. *bo = NULL;
  406. }
  407. void ast_gem_free_object(struct drm_gem_object *obj)
  408. {
  409. struct ast_bo *ast_bo = gem_to_ast_bo(obj);
  410. if (!ast_bo)
  411. return;
  412. ast_bo_unref(&ast_bo);
  413. }
  414. static inline u64 ast_bo_mmap_offset(struct ast_bo *bo)
  415. {
  416. return bo->bo.addr_space_offset;
  417. }
  418. int
  419. ast_dumb_mmap_offset(struct drm_file *file,
  420. struct drm_device *dev,
  421. uint32_t handle,
  422. uint64_t *offset)
  423. {
  424. struct drm_gem_object *obj;
  425. int ret;
  426. struct ast_bo *bo;
  427. mutex_lock(&dev->struct_mutex);
  428. obj = drm_gem_object_lookup(dev, file, handle);
  429. if (obj == NULL) {
  430. ret = -ENOENT;
  431. goto out_unlock;
  432. }
  433. bo = gem_to_ast_bo(obj);
  434. *offset = ast_bo_mmap_offset(bo);
  435. drm_gem_object_unreference(obj);
  436. ret = 0;
  437. out_unlock:
  438. mutex_unlock(&dev->struct_mutex);
  439. return ret;
  440. }