radeon_device.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <linux/console.h>
  29. #include <linux/slab.h>
  30. #include <drm/drmP.h>
  31. #include <drm/drm_crtc_helper.h>
  32. #include <drm/radeon_drm.h>
  33. #include <linux/vgaarb.h>
  34. #include <linux/vga_switcheroo.h>
  35. #include <linux/efi.h>
  36. #include "radeon_reg.h"
  37. #include "radeon.h"
  38. #include "atom.h"
  39. static const char radeon_family_name[][16] = {
  40. "R100",
  41. "RV100",
  42. "RS100",
  43. "RV200",
  44. "RS200",
  45. "R200",
  46. "RV250",
  47. "RS300",
  48. "RV280",
  49. "R300",
  50. "R350",
  51. "RV350",
  52. "RV380",
  53. "R420",
  54. "R423",
  55. "RV410",
  56. "RS400",
  57. "RS480",
  58. "RS600",
  59. "RS690",
  60. "RS740",
  61. "RV515",
  62. "R520",
  63. "RV530",
  64. "RV560",
  65. "RV570",
  66. "R580",
  67. "R600",
  68. "RV610",
  69. "RV630",
  70. "RV670",
  71. "RV620",
  72. "RV635",
  73. "RS780",
  74. "RS880",
  75. "RV770",
  76. "RV730",
  77. "RV710",
  78. "RV740",
  79. "CEDAR",
  80. "REDWOOD",
  81. "JUNIPER",
  82. "CYPRESS",
  83. "HEMLOCK",
  84. "PALM",
  85. "SUMO",
  86. "SUMO2",
  87. "BARTS",
  88. "TURKS",
  89. "CAICOS",
  90. "CAYMAN",
  91. "ARUBA",
  92. "TAHITI",
  93. "PITCAIRN",
  94. "VERDE",
  95. "OLAND",
  96. "HAINAN",
  97. "BONAIRE",
  98. "KAVERI",
  99. "KABINI",
  100. "LAST",
  101. };
  102. #if defined(CONFIG_VGA_SWITCHEROO)
  103. bool radeon_is_px(void);
  104. #else
  105. static inline bool radeon_is_px(void) { return false; }
  106. #endif
  107. /**
  108. * radeon_program_register_sequence - program an array of registers.
  109. *
  110. * @rdev: radeon_device pointer
  111. * @registers: pointer to the register array
  112. * @array_size: size of the register array
  113. *
  114. * Programs an array or registers with and and or masks.
  115. * This is a helper for setting golden registers.
  116. */
  117. void radeon_program_register_sequence(struct radeon_device *rdev,
  118. const u32 *registers,
  119. const u32 array_size)
  120. {
  121. u32 tmp, reg, and_mask, or_mask;
  122. int i;
  123. if (array_size % 3)
  124. return;
  125. for (i = 0; i < array_size; i +=3) {
  126. reg = registers[i + 0];
  127. and_mask = registers[i + 1];
  128. or_mask = registers[i + 2];
  129. if (and_mask == 0xffffffff) {
  130. tmp = or_mask;
  131. } else {
  132. tmp = RREG32(reg);
  133. tmp &= ~and_mask;
  134. tmp |= or_mask;
  135. }
  136. WREG32(reg, tmp);
  137. }
  138. }
  139. /**
  140. * radeon_surface_init - Clear GPU surface registers.
  141. *
  142. * @rdev: radeon_device pointer
  143. *
  144. * Clear GPU surface registers (r1xx-r5xx).
  145. */
  146. void radeon_surface_init(struct radeon_device *rdev)
  147. {
  148. /* FIXME: check this out */
  149. if (rdev->family < CHIP_R600) {
  150. int i;
  151. for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
  152. if (rdev->surface_regs[i].bo)
  153. radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
  154. else
  155. radeon_clear_surface_reg(rdev, i);
  156. }
  157. /* enable surfaces */
  158. WREG32(RADEON_SURFACE_CNTL, 0);
  159. }
  160. }
  161. /*
  162. * GPU scratch registers helpers function.
  163. */
  164. /**
  165. * radeon_scratch_init - Init scratch register driver information.
  166. *
  167. * @rdev: radeon_device pointer
  168. *
  169. * Init CP scratch register driver information (r1xx-r5xx)
  170. */
  171. void radeon_scratch_init(struct radeon_device *rdev)
  172. {
  173. int i;
  174. /* FIXME: check this out */
  175. if (rdev->family < CHIP_R300) {
  176. rdev->scratch.num_reg = 5;
  177. } else {
  178. rdev->scratch.num_reg = 7;
  179. }
  180. rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
  181. for (i = 0; i < rdev->scratch.num_reg; i++) {
  182. rdev->scratch.free[i] = true;
  183. rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
  184. }
  185. }
  186. /**
  187. * radeon_scratch_get - Allocate a scratch register
  188. *
  189. * @rdev: radeon_device pointer
  190. * @reg: scratch register mmio offset
  191. *
  192. * Allocate a CP scratch register for use by the driver (all asics).
  193. * Returns 0 on success or -EINVAL on failure.
  194. */
  195. int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
  196. {
  197. int i;
  198. for (i = 0; i < rdev->scratch.num_reg; i++) {
  199. if (rdev->scratch.free[i]) {
  200. rdev->scratch.free[i] = false;
  201. *reg = rdev->scratch.reg[i];
  202. return 0;
  203. }
  204. }
  205. return -EINVAL;
  206. }
  207. /**
  208. * radeon_scratch_free - Free a scratch register
  209. *
  210. * @rdev: radeon_device pointer
  211. * @reg: scratch register mmio offset
  212. *
  213. * Free a CP scratch register allocated for use by the driver (all asics)
  214. */
  215. void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
  216. {
  217. int i;
  218. for (i = 0; i < rdev->scratch.num_reg; i++) {
  219. if (rdev->scratch.reg[i] == reg) {
  220. rdev->scratch.free[i] = true;
  221. return;
  222. }
  223. }
  224. }
  225. /*
  226. * GPU doorbell aperture helpers function.
  227. */
  228. /**
  229. * radeon_doorbell_init - Init doorbell driver information.
  230. *
  231. * @rdev: radeon_device pointer
  232. *
  233. * Init doorbell driver information (CIK)
  234. * Returns 0 on success, error on failure.
  235. */
  236. int radeon_doorbell_init(struct radeon_device *rdev)
  237. {
  238. int i;
  239. /* doorbell bar mapping */
  240. rdev->doorbell.base = pci_resource_start(rdev->pdev, 2);
  241. rdev->doorbell.size = pci_resource_len(rdev->pdev, 2);
  242. /* limit to 4 MB for now */
  243. if (rdev->doorbell.size > (4 * 1024 * 1024))
  244. rdev->doorbell.size = 4 * 1024 * 1024;
  245. rdev->doorbell.ptr = ioremap(rdev->doorbell.base, rdev->doorbell.size);
  246. if (rdev->doorbell.ptr == NULL) {
  247. return -ENOMEM;
  248. }
  249. DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)rdev->doorbell.base);
  250. DRM_INFO("doorbell mmio size: %u\n", (unsigned)rdev->doorbell.size);
  251. rdev->doorbell.num_pages = rdev->doorbell.size / PAGE_SIZE;
  252. for (i = 0; i < rdev->doorbell.num_pages; i++) {
  253. rdev->doorbell.free[i] = true;
  254. }
  255. return 0;
  256. }
  257. /**
  258. * radeon_doorbell_fini - Tear down doorbell driver information.
  259. *
  260. * @rdev: radeon_device pointer
  261. *
  262. * Tear down doorbell driver information (CIK)
  263. */
  264. void radeon_doorbell_fini(struct radeon_device *rdev)
  265. {
  266. iounmap(rdev->doorbell.ptr);
  267. rdev->doorbell.ptr = NULL;
  268. }
  269. /**
  270. * radeon_doorbell_get - Allocate a doorbell page
  271. *
  272. * @rdev: radeon_device pointer
  273. * @doorbell: doorbell page number
  274. *
  275. * Allocate a doorbell page for use by the driver (all asics).
  276. * Returns 0 on success or -EINVAL on failure.
  277. */
  278. int radeon_doorbell_get(struct radeon_device *rdev, u32 *doorbell)
  279. {
  280. int i;
  281. for (i = 0; i < rdev->doorbell.num_pages; i++) {
  282. if (rdev->doorbell.free[i]) {
  283. rdev->doorbell.free[i] = false;
  284. *doorbell = i;
  285. return 0;
  286. }
  287. }
  288. return -EINVAL;
  289. }
  290. /**
  291. * radeon_doorbell_free - Free a doorbell page
  292. *
  293. * @rdev: radeon_device pointer
  294. * @doorbell: doorbell page number
  295. *
  296. * Free a doorbell page allocated for use by the driver (all asics)
  297. */
  298. void radeon_doorbell_free(struct radeon_device *rdev, u32 doorbell)
  299. {
  300. if (doorbell < rdev->doorbell.num_pages)
  301. rdev->doorbell.free[doorbell] = true;
  302. }
  303. /*
  304. * radeon_wb_*()
  305. * Writeback is the the method by which the the GPU updates special pages
  306. * in memory with the status of certain GPU events (fences, ring pointers,
  307. * etc.).
  308. */
  309. /**
  310. * radeon_wb_disable - Disable Writeback
  311. *
  312. * @rdev: radeon_device pointer
  313. *
  314. * Disables Writeback (all asics). Used for suspend.
  315. */
  316. void radeon_wb_disable(struct radeon_device *rdev)
  317. {
  318. rdev->wb.enabled = false;
  319. }
  320. /**
  321. * radeon_wb_fini - Disable Writeback and free memory
  322. *
  323. * @rdev: radeon_device pointer
  324. *
  325. * Disables Writeback and frees the Writeback memory (all asics).
  326. * Used at driver shutdown.
  327. */
  328. void radeon_wb_fini(struct radeon_device *rdev)
  329. {
  330. radeon_wb_disable(rdev);
  331. if (rdev->wb.wb_obj) {
  332. if (!radeon_bo_reserve(rdev->wb.wb_obj, false)) {
  333. radeon_bo_kunmap(rdev->wb.wb_obj);
  334. radeon_bo_unpin(rdev->wb.wb_obj);
  335. radeon_bo_unreserve(rdev->wb.wb_obj);
  336. }
  337. radeon_bo_unref(&rdev->wb.wb_obj);
  338. rdev->wb.wb = NULL;
  339. rdev->wb.wb_obj = NULL;
  340. }
  341. }
  342. /**
  343. * radeon_wb_init- Init Writeback driver info and allocate memory
  344. *
  345. * @rdev: radeon_device pointer
  346. *
  347. * Disables Writeback and frees the Writeback memory (all asics).
  348. * Used at driver startup.
  349. * Returns 0 on success or an -error on failure.
  350. */
  351. int radeon_wb_init(struct radeon_device *rdev)
  352. {
  353. int r;
  354. if (rdev->wb.wb_obj == NULL) {
  355. r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
  356. RADEON_GEM_DOMAIN_GTT, NULL, &rdev->wb.wb_obj);
  357. if (r) {
  358. dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
  359. return r;
  360. }
  361. r = radeon_bo_reserve(rdev->wb.wb_obj, false);
  362. if (unlikely(r != 0)) {
  363. radeon_wb_fini(rdev);
  364. return r;
  365. }
  366. r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
  367. &rdev->wb.gpu_addr);
  368. if (r) {
  369. radeon_bo_unreserve(rdev->wb.wb_obj);
  370. dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
  371. radeon_wb_fini(rdev);
  372. return r;
  373. }
  374. r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
  375. radeon_bo_unreserve(rdev->wb.wb_obj);
  376. if (r) {
  377. dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
  378. radeon_wb_fini(rdev);
  379. return r;
  380. }
  381. }
  382. /* clear wb memory */
  383. memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
  384. /* disable event_write fences */
  385. rdev->wb.use_event = false;
  386. /* disabled via module param */
  387. if (radeon_no_wb == 1) {
  388. rdev->wb.enabled = false;
  389. } else {
  390. if (rdev->flags & RADEON_IS_AGP) {
  391. /* often unreliable on AGP */
  392. rdev->wb.enabled = false;
  393. } else if (rdev->family < CHIP_R300) {
  394. /* often unreliable on pre-r300 */
  395. rdev->wb.enabled = false;
  396. } else {
  397. rdev->wb.enabled = true;
  398. /* event_write fences are only available on r600+ */
  399. if (rdev->family >= CHIP_R600) {
  400. rdev->wb.use_event = true;
  401. }
  402. }
  403. }
  404. /* always use writeback/events on NI, APUs */
  405. if (rdev->family >= CHIP_PALM) {
  406. rdev->wb.enabled = true;
  407. rdev->wb.use_event = true;
  408. }
  409. dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
  410. return 0;
  411. }
  412. /**
  413. * radeon_vram_location - try to find VRAM location
  414. * @rdev: radeon device structure holding all necessary informations
  415. * @mc: memory controller structure holding memory informations
  416. * @base: base address at which to put VRAM
  417. *
  418. * Function will place try to place VRAM at base address provided
  419. * as parameter (which is so far either PCI aperture address or
  420. * for IGP TOM base address).
  421. *
  422. * If there is not enough space to fit the unvisible VRAM in the 32bits
  423. * address space then we limit the VRAM size to the aperture.
  424. *
  425. * If we are using AGP and if the AGP aperture doesn't allow us to have
  426. * room for all the VRAM than we restrict the VRAM to the PCI aperture
  427. * size and print a warning.
  428. *
  429. * This function will never fails, worst case are limiting VRAM.
  430. *
  431. * Note: GTT start, end, size should be initialized before calling this
  432. * function on AGP platform.
  433. *
  434. * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
  435. * this shouldn't be a problem as we are using the PCI aperture as a reference.
  436. * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
  437. * not IGP.
  438. *
  439. * Note: we use mc_vram_size as on some board we need to program the mc to
  440. * cover the whole aperture even if VRAM size is inferior to aperture size
  441. * Novell bug 204882 + along with lots of ubuntu ones
  442. *
  443. * Note: when limiting vram it's safe to overwritte real_vram_size because
  444. * we are not in case where real_vram_size is inferior to mc_vram_size (ie
  445. * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
  446. * ones)
  447. *
  448. * Note: IGP TOM addr should be the same as the aperture addr, we don't
  449. * explicitly check for that thought.
  450. *
  451. * FIXME: when reducing VRAM size align new size on power of 2.
  452. */
  453. void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
  454. {
  455. uint64_t limit = (uint64_t)radeon_vram_limit << 20;
  456. mc->vram_start = base;
  457. if (mc->mc_vram_size > (rdev->mc.mc_mask - base + 1)) {
  458. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  459. mc->real_vram_size = mc->aper_size;
  460. mc->mc_vram_size = mc->aper_size;
  461. }
  462. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  463. if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
  464. dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
  465. mc->real_vram_size = mc->aper_size;
  466. mc->mc_vram_size = mc->aper_size;
  467. }
  468. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  469. if (limit && limit < mc->real_vram_size)
  470. mc->real_vram_size = limit;
  471. dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
  472. mc->mc_vram_size >> 20, mc->vram_start,
  473. mc->vram_end, mc->real_vram_size >> 20);
  474. }
  475. /**
  476. * radeon_gtt_location - try to find GTT location
  477. * @rdev: radeon device structure holding all necessary informations
  478. * @mc: memory controller structure holding memory informations
  479. *
  480. * Function will place try to place GTT before or after VRAM.
  481. *
  482. * If GTT size is bigger than space left then we ajust GTT size.
  483. * Thus function will never fails.
  484. *
  485. * FIXME: when reducing GTT size align new size on power of 2.
  486. */
  487. void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
  488. {
  489. u64 size_af, size_bf;
  490. size_af = ((rdev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
  491. size_bf = mc->vram_start & ~mc->gtt_base_align;
  492. if (size_bf > size_af) {
  493. if (mc->gtt_size > size_bf) {
  494. dev_warn(rdev->dev, "limiting GTT\n");
  495. mc->gtt_size = size_bf;
  496. }
  497. mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
  498. } else {
  499. if (mc->gtt_size > size_af) {
  500. dev_warn(rdev->dev, "limiting GTT\n");
  501. mc->gtt_size = size_af;
  502. }
  503. mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
  504. }
  505. mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
  506. dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
  507. mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
  508. }
  509. /*
  510. * GPU helpers function.
  511. */
  512. /**
  513. * radeon_card_posted - check if the hw has already been initialized
  514. *
  515. * @rdev: radeon_device pointer
  516. *
  517. * Check if the asic has been initialized (all asics).
  518. * Used at driver startup.
  519. * Returns true if initialized or false if not.
  520. */
  521. bool radeon_card_posted(struct radeon_device *rdev)
  522. {
  523. uint32_t reg;
  524. /* required for EFI mode on macbook2,1 which uses an r5xx asic */
  525. if (efi_enabled(EFI_BOOT) &&
  526. (rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) &&
  527. (rdev->family < CHIP_R600))
  528. return false;
  529. if (ASIC_IS_NODCE(rdev))
  530. goto check_memsize;
  531. /* first check CRTCs */
  532. if (ASIC_IS_DCE4(rdev)) {
  533. reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
  534. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
  535. if (rdev->num_crtc >= 4) {
  536. reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
  537. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET);
  538. }
  539. if (rdev->num_crtc >= 6) {
  540. reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
  541. RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
  542. }
  543. if (reg & EVERGREEN_CRTC_MASTER_EN)
  544. return true;
  545. } else if (ASIC_IS_AVIVO(rdev)) {
  546. reg = RREG32(AVIVO_D1CRTC_CONTROL) |
  547. RREG32(AVIVO_D2CRTC_CONTROL);
  548. if (reg & AVIVO_CRTC_EN) {
  549. return true;
  550. }
  551. } else {
  552. reg = RREG32(RADEON_CRTC_GEN_CNTL) |
  553. RREG32(RADEON_CRTC2_GEN_CNTL);
  554. if (reg & RADEON_CRTC_EN) {
  555. return true;
  556. }
  557. }
  558. check_memsize:
  559. /* then check MEM_SIZE, in case the crtcs are off */
  560. if (rdev->family >= CHIP_R600)
  561. reg = RREG32(R600_CONFIG_MEMSIZE);
  562. else
  563. reg = RREG32(RADEON_CONFIG_MEMSIZE);
  564. if (reg)
  565. return true;
  566. return false;
  567. }
  568. /**
  569. * radeon_update_bandwidth_info - update display bandwidth params
  570. *
  571. * @rdev: radeon_device pointer
  572. *
  573. * Used when sclk/mclk are switched or display modes are set.
  574. * params are used to calculate display watermarks (all asics)
  575. */
  576. void radeon_update_bandwidth_info(struct radeon_device *rdev)
  577. {
  578. fixed20_12 a;
  579. u32 sclk = rdev->pm.current_sclk;
  580. u32 mclk = rdev->pm.current_mclk;
  581. /* sclk/mclk in Mhz */
  582. a.full = dfixed_const(100);
  583. rdev->pm.sclk.full = dfixed_const(sclk);
  584. rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
  585. rdev->pm.mclk.full = dfixed_const(mclk);
  586. rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
  587. if (rdev->flags & RADEON_IS_IGP) {
  588. a.full = dfixed_const(16);
  589. /* core_bandwidth = sclk(Mhz) * 16 */
  590. rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
  591. }
  592. }
  593. /**
  594. * radeon_boot_test_post_card - check and possibly initialize the hw
  595. *
  596. * @rdev: radeon_device pointer
  597. *
  598. * Check if the asic is initialized and if not, attempt to initialize
  599. * it (all asics).
  600. * Returns true if initialized or false if not.
  601. */
  602. bool radeon_boot_test_post_card(struct radeon_device *rdev)
  603. {
  604. if (radeon_card_posted(rdev))
  605. return true;
  606. if (rdev->bios) {
  607. DRM_INFO("GPU not posted. posting now...\n");
  608. if (rdev->is_atom_bios)
  609. atom_asic_init(rdev->mode_info.atom_context);
  610. else
  611. radeon_combios_asic_init(rdev->ddev);
  612. return true;
  613. } else {
  614. dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
  615. return false;
  616. }
  617. }
  618. /**
  619. * radeon_dummy_page_init - init dummy page used by the driver
  620. *
  621. * @rdev: radeon_device pointer
  622. *
  623. * Allocate the dummy page used by the driver (all asics).
  624. * This dummy page is used by the driver as a filler for gart entries
  625. * when pages are taken out of the GART
  626. * Returns 0 on sucess, -ENOMEM on failure.
  627. */
  628. int radeon_dummy_page_init(struct radeon_device *rdev)
  629. {
  630. if (rdev->dummy_page.page)
  631. return 0;
  632. rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  633. if (rdev->dummy_page.page == NULL)
  634. return -ENOMEM;
  635. rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
  636. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  637. if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
  638. dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
  639. __free_page(rdev->dummy_page.page);
  640. rdev->dummy_page.page = NULL;
  641. return -ENOMEM;
  642. }
  643. return 0;
  644. }
  645. /**
  646. * radeon_dummy_page_fini - free dummy page used by the driver
  647. *
  648. * @rdev: radeon_device pointer
  649. *
  650. * Frees the dummy page used by the driver (all asics).
  651. */
  652. void radeon_dummy_page_fini(struct radeon_device *rdev)
  653. {
  654. if (rdev->dummy_page.page == NULL)
  655. return;
  656. pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
  657. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  658. __free_page(rdev->dummy_page.page);
  659. rdev->dummy_page.page = NULL;
  660. }
  661. /* ATOM accessor methods */
  662. /*
  663. * ATOM is an interpreted byte code stored in tables in the vbios. The
  664. * driver registers callbacks to access registers and the interpreter
  665. * in the driver parses the tables and executes then to program specific
  666. * actions (set display modes, asic init, etc.). See radeon_atombios.c,
  667. * atombios.h, and atom.c
  668. */
  669. /**
  670. * cail_pll_read - read PLL register
  671. *
  672. * @info: atom card_info pointer
  673. * @reg: PLL register offset
  674. *
  675. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  676. * Returns the value of the PLL register.
  677. */
  678. static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
  679. {
  680. struct radeon_device *rdev = info->dev->dev_private;
  681. uint32_t r;
  682. r = rdev->pll_rreg(rdev, reg);
  683. return r;
  684. }
  685. /**
  686. * cail_pll_write - write PLL register
  687. *
  688. * @info: atom card_info pointer
  689. * @reg: PLL register offset
  690. * @val: value to write to the pll register
  691. *
  692. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  693. */
  694. static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
  695. {
  696. struct radeon_device *rdev = info->dev->dev_private;
  697. rdev->pll_wreg(rdev, reg, val);
  698. }
  699. /**
  700. * cail_mc_read - read MC (Memory Controller) register
  701. *
  702. * @info: atom card_info pointer
  703. * @reg: MC register offset
  704. *
  705. * Provides an MC register accessor for the atom interpreter (r4xx+).
  706. * Returns the value of the MC register.
  707. */
  708. static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
  709. {
  710. struct radeon_device *rdev = info->dev->dev_private;
  711. uint32_t r;
  712. r = rdev->mc_rreg(rdev, reg);
  713. return r;
  714. }
  715. /**
  716. * cail_mc_write - write MC (Memory Controller) register
  717. *
  718. * @info: atom card_info pointer
  719. * @reg: MC register offset
  720. * @val: value to write to the pll register
  721. *
  722. * Provides a MC register accessor for the atom interpreter (r4xx+).
  723. */
  724. static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
  725. {
  726. struct radeon_device *rdev = info->dev->dev_private;
  727. rdev->mc_wreg(rdev, reg, val);
  728. }
  729. /**
  730. * cail_reg_write - write MMIO register
  731. *
  732. * @info: atom card_info pointer
  733. * @reg: MMIO register offset
  734. * @val: value to write to the pll register
  735. *
  736. * Provides a MMIO register accessor for the atom interpreter (r4xx+).
  737. */
  738. static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
  739. {
  740. struct radeon_device *rdev = info->dev->dev_private;
  741. WREG32(reg*4, val);
  742. }
  743. /**
  744. * cail_reg_read - read MMIO register
  745. *
  746. * @info: atom card_info pointer
  747. * @reg: MMIO register offset
  748. *
  749. * Provides an MMIO register accessor for the atom interpreter (r4xx+).
  750. * Returns the value of the MMIO register.
  751. */
  752. static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
  753. {
  754. struct radeon_device *rdev = info->dev->dev_private;
  755. uint32_t r;
  756. r = RREG32(reg*4);
  757. return r;
  758. }
  759. /**
  760. * cail_ioreg_write - write IO register
  761. *
  762. * @info: atom card_info pointer
  763. * @reg: IO register offset
  764. * @val: value to write to the pll register
  765. *
  766. * Provides a IO register accessor for the atom interpreter (r4xx+).
  767. */
  768. static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
  769. {
  770. struct radeon_device *rdev = info->dev->dev_private;
  771. WREG32_IO(reg*4, val);
  772. }
  773. /**
  774. * cail_ioreg_read - read IO register
  775. *
  776. * @info: atom card_info pointer
  777. * @reg: IO register offset
  778. *
  779. * Provides an IO register accessor for the atom interpreter (r4xx+).
  780. * Returns the value of the IO register.
  781. */
  782. static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
  783. {
  784. struct radeon_device *rdev = info->dev->dev_private;
  785. uint32_t r;
  786. r = RREG32_IO(reg*4);
  787. return r;
  788. }
  789. /**
  790. * radeon_atombios_init - init the driver info and callbacks for atombios
  791. *
  792. * @rdev: radeon_device pointer
  793. *
  794. * Initializes the driver info and register access callbacks for the
  795. * ATOM interpreter (r4xx+).
  796. * Returns 0 on sucess, -ENOMEM on failure.
  797. * Called at driver startup.
  798. */
  799. int radeon_atombios_init(struct radeon_device *rdev)
  800. {
  801. struct card_info *atom_card_info =
  802. kzalloc(sizeof(struct card_info), GFP_KERNEL);
  803. if (!atom_card_info)
  804. return -ENOMEM;
  805. rdev->mode_info.atom_card_info = atom_card_info;
  806. atom_card_info->dev = rdev->ddev;
  807. atom_card_info->reg_read = cail_reg_read;
  808. atom_card_info->reg_write = cail_reg_write;
  809. /* needed for iio ops */
  810. if (rdev->rio_mem) {
  811. atom_card_info->ioreg_read = cail_ioreg_read;
  812. atom_card_info->ioreg_write = cail_ioreg_write;
  813. } else {
  814. DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
  815. atom_card_info->ioreg_read = cail_reg_read;
  816. atom_card_info->ioreg_write = cail_reg_write;
  817. }
  818. atom_card_info->mc_read = cail_mc_read;
  819. atom_card_info->mc_write = cail_mc_write;
  820. atom_card_info->pll_read = cail_pll_read;
  821. atom_card_info->pll_write = cail_pll_write;
  822. rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
  823. if (!rdev->mode_info.atom_context) {
  824. radeon_atombios_fini(rdev);
  825. return -ENOMEM;
  826. }
  827. mutex_init(&rdev->mode_info.atom_context->mutex);
  828. radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
  829. atom_allocate_fb_scratch(rdev->mode_info.atom_context);
  830. return 0;
  831. }
  832. /**
  833. * radeon_atombios_fini - free the driver info and callbacks for atombios
  834. *
  835. * @rdev: radeon_device pointer
  836. *
  837. * Frees the driver info and register access callbacks for the ATOM
  838. * interpreter (r4xx+).
  839. * Called at driver shutdown.
  840. */
  841. void radeon_atombios_fini(struct radeon_device *rdev)
  842. {
  843. if (rdev->mode_info.atom_context) {
  844. kfree(rdev->mode_info.atom_context->scratch);
  845. }
  846. kfree(rdev->mode_info.atom_context);
  847. rdev->mode_info.atom_context = NULL;
  848. kfree(rdev->mode_info.atom_card_info);
  849. rdev->mode_info.atom_card_info = NULL;
  850. }
  851. /* COMBIOS */
  852. /*
  853. * COMBIOS is the bios format prior to ATOM. It provides
  854. * command tables similar to ATOM, but doesn't have a unified
  855. * parser. See radeon_combios.c
  856. */
  857. /**
  858. * radeon_combios_init - init the driver info for combios
  859. *
  860. * @rdev: radeon_device pointer
  861. *
  862. * Initializes the driver info for combios (r1xx-r3xx).
  863. * Returns 0 on sucess.
  864. * Called at driver startup.
  865. */
  866. int radeon_combios_init(struct radeon_device *rdev)
  867. {
  868. radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
  869. return 0;
  870. }
  871. /**
  872. * radeon_combios_fini - free the driver info for combios
  873. *
  874. * @rdev: radeon_device pointer
  875. *
  876. * Frees the driver info for combios (r1xx-r3xx).
  877. * Called at driver shutdown.
  878. */
  879. void radeon_combios_fini(struct radeon_device *rdev)
  880. {
  881. }
  882. /* if we get transitioned to only one device, take VGA back */
  883. /**
  884. * radeon_vga_set_decode - enable/disable vga decode
  885. *
  886. * @cookie: radeon_device pointer
  887. * @state: enable/disable vga decode
  888. *
  889. * Enable/disable vga decode (all asics).
  890. * Returns VGA resource flags.
  891. */
  892. static unsigned int radeon_vga_set_decode(void *cookie, bool state)
  893. {
  894. struct radeon_device *rdev = cookie;
  895. radeon_vga_set_state(rdev, state);
  896. if (state)
  897. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  898. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  899. else
  900. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  901. }
  902. /**
  903. * radeon_check_pot_argument - check that argument is a power of two
  904. *
  905. * @arg: value to check
  906. *
  907. * Validates that a certain argument is a power of two (all asics).
  908. * Returns true if argument is valid.
  909. */
  910. static bool radeon_check_pot_argument(int arg)
  911. {
  912. return (arg & (arg - 1)) == 0;
  913. }
  914. /**
  915. * radeon_check_arguments - validate module params
  916. *
  917. * @rdev: radeon_device pointer
  918. *
  919. * Validates certain module parameters and updates
  920. * the associated values used by the driver (all asics).
  921. */
  922. static void radeon_check_arguments(struct radeon_device *rdev)
  923. {
  924. /* vramlimit must be a power of two */
  925. if (!radeon_check_pot_argument(radeon_vram_limit)) {
  926. dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
  927. radeon_vram_limit);
  928. radeon_vram_limit = 0;
  929. }
  930. if (radeon_gart_size == -1) {
  931. /* default to a larger gart size on newer asics */
  932. if (rdev->family >= CHIP_RV770)
  933. radeon_gart_size = 1024;
  934. else
  935. radeon_gart_size = 512;
  936. }
  937. /* gtt size must be power of two and greater or equal to 32M */
  938. if (radeon_gart_size < 32) {
  939. dev_warn(rdev->dev, "gart size (%d) too small\n",
  940. radeon_gart_size);
  941. if (rdev->family >= CHIP_RV770)
  942. radeon_gart_size = 1024;
  943. else
  944. radeon_gart_size = 512;
  945. } else if (!radeon_check_pot_argument(radeon_gart_size)) {
  946. dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
  947. radeon_gart_size);
  948. if (rdev->family >= CHIP_RV770)
  949. radeon_gart_size = 1024;
  950. else
  951. radeon_gart_size = 512;
  952. }
  953. rdev->mc.gtt_size = (uint64_t)radeon_gart_size << 20;
  954. /* AGP mode can only be -1, 1, 2, 4, 8 */
  955. switch (radeon_agpmode) {
  956. case -1:
  957. case 0:
  958. case 1:
  959. case 2:
  960. case 4:
  961. case 8:
  962. break;
  963. default:
  964. dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
  965. "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
  966. radeon_agpmode = 0;
  967. break;
  968. }
  969. }
  970. /**
  971. * radeon_switcheroo_quirk_long_wakeup - return true if longer d3 delay is
  972. * needed for waking up.
  973. *
  974. * @pdev: pci dev pointer
  975. */
  976. static bool radeon_switcheroo_quirk_long_wakeup(struct pci_dev *pdev)
  977. {
  978. /* 6600m in a macbook pro */
  979. if (pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE &&
  980. pdev->subsystem_device == 0x00e2) {
  981. printk(KERN_INFO "radeon: quirking longer d3 wakeup delay\n");
  982. return true;
  983. }
  984. return false;
  985. }
  986. /**
  987. * radeon_switcheroo_set_state - set switcheroo state
  988. *
  989. * @pdev: pci dev pointer
  990. * @state: vga switcheroo state
  991. *
  992. * Callback for the switcheroo driver. Suspends or resumes the
  993. * the asics before or after it is powered up using ACPI methods.
  994. */
  995. static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  996. {
  997. struct drm_device *dev = pci_get_drvdata(pdev);
  998. if (radeon_is_px() && state == VGA_SWITCHEROO_OFF)
  999. return;
  1000. if (state == VGA_SWITCHEROO_ON) {
  1001. unsigned d3_delay = dev->pdev->d3_delay;
  1002. printk(KERN_INFO "radeon: switched on\n");
  1003. /* don't suspend or resume card normally */
  1004. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  1005. if (d3_delay < 20 && radeon_switcheroo_quirk_long_wakeup(pdev))
  1006. dev->pdev->d3_delay = 20;
  1007. radeon_resume_kms(dev, true, true);
  1008. dev->pdev->d3_delay = d3_delay;
  1009. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  1010. drm_kms_helper_poll_enable(dev);
  1011. } else {
  1012. printk(KERN_INFO "radeon: switched off\n");
  1013. drm_kms_helper_poll_disable(dev);
  1014. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  1015. radeon_suspend_kms(dev, true, true);
  1016. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  1017. }
  1018. }
  1019. /**
  1020. * radeon_switcheroo_can_switch - see if switcheroo state can change
  1021. *
  1022. * @pdev: pci dev pointer
  1023. *
  1024. * Callback for the switcheroo driver. Check of the switcheroo
  1025. * state can be changed.
  1026. * Returns true if the state can be changed, false if not.
  1027. */
  1028. static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
  1029. {
  1030. struct drm_device *dev = pci_get_drvdata(pdev);
  1031. bool can_switch;
  1032. spin_lock(&dev->count_lock);
  1033. can_switch = (dev->open_count == 0);
  1034. spin_unlock(&dev->count_lock);
  1035. return can_switch;
  1036. }
  1037. static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
  1038. .set_gpu_state = radeon_switcheroo_set_state,
  1039. .reprobe = NULL,
  1040. .can_switch = radeon_switcheroo_can_switch,
  1041. };
  1042. /**
  1043. * radeon_device_init - initialize the driver
  1044. *
  1045. * @rdev: radeon_device pointer
  1046. * @pdev: drm dev pointer
  1047. * @pdev: pci dev pointer
  1048. * @flags: driver flags
  1049. *
  1050. * Initializes the driver info and hw (all asics).
  1051. * Returns 0 for success or an error on failure.
  1052. * Called at driver startup.
  1053. */
  1054. int radeon_device_init(struct radeon_device *rdev,
  1055. struct drm_device *ddev,
  1056. struct pci_dev *pdev,
  1057. uint32_t flags)
  1058. {
  1059. int r, i;
  1060. int dma_bits;
  1061. bool runtime = false;
  1062. rdev->shutdown = false;
  1063. rdev->dev = &pdev->dev;
  1064. rdev->ddev = ddev;
  1065. rdev->pdev = pdev;
  1066. rdev->flags = flags;
  1067. rdev->family = flags & RADEON_FAMILY_MASK;
  1068. rdev->is_atom_bios = false;
  1069. rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
  1070. rdev->mc.gtt_size = 512 * 1024 * 1024;
  1071. rdev->accel_working = false;
  1072. /* set up ring ids */
  1073. for (i = 0; i < RADEON_NUM_RINGS; i++) {
  1074. rdev->ring[i].idx = i;
  1075. }
  1076. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n",
  1077. radeon_family_name[rdev->family], pdev->vendor, pdev->device,
  1078. pdev->subsystem_vendor, pdev->subsystem_device);
  1079. /* mutex initialization are all done here so we
  1080. * can recall function without having locking issues */
  1081. mutex_init(&rdev->ring_lock);
  1082. mutex_init(&rdev->dc_hw_i2c_mutex);
  1083. atomic_set(&rdev->ih.lock, 0);
  1084. mutex_init(&rdev->gem.mutex);
  1085. mutex_init(&rdev->pm.mutex);
  1086. mutex_init(&rdev->gpu_clock_mutex);
  1087. mutex_init(&rdev->srbm_mutex);
  1088. init_rwsem(&rdev->pm.mclk_lock);
  1089. init_rwsem(&rdev->exclusive_lock);
  1090. init_waitqueue_head(&rdev->irq.vblank_queue);
  1091. r = radeon_gem_init(rdev);
  1092. if (r)
  1093. return r;
  1094. /* initialize vm here */
  1095. mutex_init(&rdev->vm_manager.lock);
  1096. /* Adjust VM size here.
  1097. * Currently set to 4GB ((1 << 20) 4k pages).
  1098. * Max GPUVM size for cayman and SI is 40 bits.
  1099. */
  1100. rdev->vm_manager.max_pfn = 1 << 20;
  1101. INIT_LIST_HEAD(&rdev->vm_manager.lru_vm);
  1102. /* Set asic functions */
  1103. r = radeon_asic_init(rdev);
  1104. if (r)
  1105. return r;
  1106. radeon_check_arguments(rdev);
  1107. /* all of the newer IGP chips have an internal gart
  1108. * However some rs4xx report as AGP, so remove that here.
  1109. */
  1110. if ((rdev->family >= CHIP_RS400) &&
  1111. (rdev->flags & RADEON_IS_IGP)) {
  1112. rdev->flags &= ~RADEON_IS_AGP;
  1113. }
  1114. if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
  1115. radeon_agp_disable(rdev);
  1116. }
  1117. /* Set the internal MC address mask
  1118. * This is the max address of the GPU's
  1119. * internal address space.
  1120. */
  1121. if (rdev->family >= CHIP_CAYMAN)
  1122. rdev->mc.mc_mask = 0xffffffffffULL; /* 40 bit MC */
  1123. else if (rdev->family >= CHIP_CEDAR)
  1124. rdev->mc.mc_mask = 0xfffffffffULL; /* 36 bit MC */
  1125. else
  1126. rdev->mc.mc_mask = 0xffffffffULL; /* 32 bit MC */
  1127. /* set DMA mask + need_dma32 flags.
  1128. * PCIE - can handle 40-bits.
  1129. * IGP - can handle 40-bits
  1130. * AGP - generally dma32 is safest
  1131. * PCI - dma32 for legacy pci gart, 40 bits on newer asics
  1132. */
  1133. rdev->need_dma32 = false;
  1134. if (rdev->flags & RADEON_IS_AGP)
  1135. rdev->need_dma32 = true;
  1136. if ((rdev->flags & RADEON_IS_PCI) &&
  1137. (rdev->family <= CHIP_RS740))
  1138. rdev->need_dma32 = true;
  1139. dma_bits = rdev->need_dma32 ? 32 : 40;
  1140. r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  1141. if (r) {
  1142. rdev->need_dma32 = true;
  1143. dma_bits = 32;
  1144. printk(KERN_WARNING "radeon: No suitable DMA available.\n");
  1145. }
  1146. r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
  1147. if (r) {
  1148. pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
  1149. printk(KERN_WARNING "radeon: No coherent DMA available.\n");
  1150. }
  1151. /* Registers mapping */
  1152. /* TODO: block userspace mapping of io register */
  1153. spin_lock_init(&rdev->mmio_idx_lock);
  1154. spin_lock_init(&rdev->smc_idx_lock);
  1155. spin_lock_init(&rdev->pll_idx_lock);
  1156. spin_lock_init(&rdev->mc_idx_lock);
  1157. spin_lock_init(&rdev->pcie_idx_lock);
  1158. spin_lock_init(&rdev->pciep_idx_lock);
  1159. spin_lock_init(&rdev->pif_idx_lock);
  1160. spin_lock_init(&rdev->cg_idx_lock);
  1161. spin_lock_init(&rdev->uvd_idx_lock);
  1162. spin_lock_init(&rdev->rcu_idx_lock);
  1163. spin_lock_init(&rdev->didt_idx_lock);
  1164. spin_lock_init(&rdev->end_idx_lock);
  1165. if (rdev->family >= CHIP_BONAIRE) {
  1166. rdev->rmmio_base = pci_resource_start(rdev->pdev, 5);
  1167. rdev->rmmio_size = pci_resource_len(rdev->pdev, 5);
  1168. } else {
  1169. rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
  1170. rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
  1171. }
  1172. rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
  1173. if (rdev->rmmio == NULL) {
  1174. return -ENOMEM;
  1175. }
  1176. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
  1177. DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
  1178. /* doorbell bar mapping */
  1179. if (rdev->family >= CHIP_BONAIRE)
  1180. radeon_doorbell_init(rdev);
  1181. /* io port mapping */
  1182. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1183. if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
  1184. rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
  1185. rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
  1186. break;
  1187. }
  1188. }
  1189. if (rdev->rio_mem == NULL)
  1190. DRM_ERROR("Unable to find PCI I/O BAR\n");
  1191. /* if we have > 1 VGA cards, then disable the radeon VGA resources */
  1192. /* this will fail for cards that aren't VGA class devices, just
  1193. * ignore it */
  1194. vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
  1195. if (radeon_runtime_pm == 1)
  1196. runtime = true;
  1197. if ((radeon_runtime_pm == -1) && radeon_is_px())
  1198. runtime = true;
  1199. vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops, runtime);
  1200. if (runtime)
  1201. vga_switcheroo_init_domain_pm_ops(rdev->dev, &rdev->vga_pm_domain);
  1202. r = radeon_init(rdev);
  1203. if (r)
  1204. return r;
  1205. r = radeon_ib_ring_tests(rdev);
  1206. if (r)
  1207. DRM_ERROR("ib ring test failed (%d).\n", r);
  1208. r = radeon_gem_debugfs_init(rdev);
  1209. if (r) {
  1210. DRM_ERROR("registering gem debugfs failed (%d).\n", r);
  1211. }
  1212. if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
  1213. /* Acceleration not working on AGP card try again
  1214. * with fallback to PCI or PCIE GART
  1215. */
  1216. radeon_asic_reset(rdev);
  1217. radeon_fini(rdev);
  1218. radeon_agp_disable(rdev);
  1219. r = radeon_init(rdev);
  1220. if (r)
  1221. return r;
  1222. }
  1223. if ((radeon_testing & 1)) {
  1224. if (rdev->accel_working)
  1225. radeon_test_moves(rdev);
  1226. else
  1227. DRM_INFO("radeon: acceleration disabled, skipping move tests\n");
  1228. }
  1229. if ((radeon_testing & 2)) {
  1230. if (rdev->accel_working)
  1231. radeon_test_syncing(rdev);
  1232. else
  1233. DRM_INFO("radeon: acceleration disabled, skipping sync tests\n");
  1234. }
  1235. if (radeon_benchmarking) {
  1236. if (rdev->accel_working)
  1237. radeon_benchmark(rdev, radeon_benchmarking);
  1238. else
  1239. DRM_INFO("radeon: acceleration disabled, skipping benchmarks\n");
  1240. }
  1241. return 0;
  1242. }
  1243. static void radeon_debugfs_remove_files(struct radeon_device *rdev);
  1244. /**
  1245. * radeon_device_fini - tear down the driver
  1246. *
  1247. * @rdev: radeon_device pointer
  1248. *
  1249. * Tear down the driver info (all asics).
  1250. * Called at driver shutdown.
  1251. */
  1252. void radeon_device_fini(struct radeon_device *rdev)
  1253. {
  1254. DRM_INFO("radeon: finishing device.\n");
  1255. rdev->shutdown = true;
  1256. /* evict vram memory */
  1257. radeon_bo_evict_vram(rdev);
  1258. radeon_fini(rdev);
  1259. vga_switcheroo_unregister_client(rdev->pdev);
  1260. vga_client_register(rdev->pdev, NULL, NULL, NULL);
  1261. if (rdev->rio_mem)
  1262. pci_iounmap(rdev->pdev, rdev->rio_mem);
  1263. rdev->rio_mem = NULL;
  1264. iounmap(rdev->rmmio);
  1265. rdev->rmmio = NULL;
  1266. if (rdev->family >= CHIP_BONAIRE)
  1267. radeon_doorbell_fini(rdev);
  1268. radeon_debugfs_remove_files(rdev);
  1269. }
  1270. /*
  1271. * Suspend & resume.
  1272. */
  1273. /**
  1274. * radeon_suspend_kms - initiate device suspend
  1275. *
  1276. * @pdev: drm dev pointer
  1277. * @state: suspend state
  1278. *
  1279. * Puts the hw in the suspend state (all asics).
  1280. * Returns 0 for success or an error on failure.
  1281. * Called at driver suspend.
  1282. */
  1283. int radeon_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon)
  1284. {
  1285. struct radeon_device *rdev;
  1286. struct drm_crtc *crtc;
  1287. struct drm_connector *connector;
  1288. int i, r;
  1289. bool force_completion = false;
  1290. if (dev == NULL || dev->dev_private == NULL) {
  1291. return -ENODEV;
  1292. }
  1293. rdev = dev->dev_private;
  1294. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1295. return 0;
  1296. drm_kms_helper_poll_disable(dev);
  1297. /* turn off display hw */
  1298. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1299. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  1300. }
  1301. /* unpin the front buffers */
  1302. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1303. struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
  1304. struct radeon_bo *robj;
  1305. if (rfb == NULL || rfb->obj == NULL) {
  1306. continue;
  1307. }
  1308. robj = gem_to_radeon_bo(rfb->obj);
  1309. /* don't unpin kernel fb objects */
  1310. if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
  1311. r = radeon_bo_reserve(robj, false);
  1312. if (r == 0) {
  1313. radeon_bo_unpin(robj);
  1314. radeon_bo_unreserve(robj);
  1315. }
  1316. }
  1317. }
  1318. /* evict vram memory */
  1319. radeon_bo_evict_vram(rdev);
  1320. mutex_lock(&rdev->ring_lock);
  1321. /* wait for gpu to finish processing current batch */
  1322. for (i = 0; i < RADEON_NUM_RINGS; i++) {
  1323. r = radeon_fence_wait_empty_locked(rdev, i);
  1324. if (r) {
  1325. /* delay GPU reset to resume */
  1326. force_completion = true;
  1327. }
  1328. }
  1329. if (force_completion) {
  1330. radeon_fence_driver_force_completion(rdev);
  1331. }
  1332. mutex_unlock(&rdev->ring_lock);
  1333. radeon_save_bios_scratch_regs(rdev);
  1334. radeon_pm_suspend(rdev);
  1335. radeon_suspend(rdev);
  1336. radeon_hpd_fini(rdev);
  1337. /* evict remaining vram memory */
  1338. radeon_bo_evict_vram(rdev);
  1339. radeon_agp_suspend(rdev);
  1340. pci_save_state(dev->pdev);
  1341. if (suspend) {
  1342. /* Shut down the device */
  1343. pci_disable_device(dev->pdev);
  1344. pci_set_power_state(dev->pdev, PCI_D3hot);
  1345. }
  1346. if (fbcon) {
  1347. console_lock();
  1348. radeon_fbdev_set_suspend(rdev, 1);
  1349. console_unlock();
  1350. }
  1351. return 0;
  1352. }
  1353. /**
  1354. * radeon_resume_kms - initiate device resume
  1355. *
  1356. * @pdev: drm dev pointer
  1357. *
  1358. * Bring the hw back to operating state (all asics).
  1359. * Returns 0 for success or an error on failure.
  1360. * Called at driver resume.
  1361. */
  1362. int radeon_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
  1363. {
  1364. struct drm_connector *connector;
  1365. struct radeon_device *rdev = dev->dev_private;
  1366. int r;
  1367. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1368. return 0;
  1369. if (fbcon) {
  1370. console_lock();
  1371. }
  1372. if (resume) {
  1373. pci_set_power_state(dev->pdev, PCI_D0);
  1374. pci_restore_state(dev->pdev);
  1375. if (pci_enable_device(dev->pdev)) {
  1376. if (fbcon)
  1377. console_unlock();
  1378. return -1;
  1379. }
  1380. }
  1381. /* resume AGP if in use */
  1382. radeon_agp_resume(rdev);
  1383. radeon_resume(rdev);
  1384. r = radeon_ib_ring_tests(rdev);
  1385. if (r)
  1386. DRM_ERROR("ib ring test failed (%d).\n", r);
  1387. radeon_pm_resume(rdev);
  1388. radeon_restore_bios_scratch_regs(rdev);
  1389. if (fbcon) {
  1390. radeon_fbdev_set_suspend(rdev, 0);
  1391. console_unlock();
  1392. }
  1393. /* init dig PHYs, disp eng pll */
  1394. if (rdev->is_atom_bios) {
  1395. radeon_atom_encoder_init(rdev);
  1396. radeon_atom_disp_eng_pll_init(rdev);
  1397. /* turn on the BL */
  1398. if (rdev->mode_info.bl_encoder) {
  1399. u8 bl_level = radeon_get_backlight_level(rdev,
  1400. rdev->mode_info.bl_encoder);
  1401. radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
  1402. bl_level);
  1403. }
  1404. }
  1405. /* reset hpd state */
  1406. radeon_hpd_init(rdev);
  1407. /* blat the mode back in */
  1408. drm_helper_resume_force_mode(dev);
  1409. /* turn on display hw */
  1410. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1411. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  1412. }
  1413. drm_kms_helper_poll_enable(dev);
  1414. return 0;
  1415. }
  1416. /**
  1417. * radeon_gpu_reset - reset the asic
  1418. *
  1419. * @rdev: radeon device pointer
  1420. *
  1421. * Attempt the reset the GPU if it has hung (all asics).
  1422. * Returns 0 for success or an error on failure.
  1423. */
  1424. int radeon_gpu_reset(struct radeon_device *rdev)
  1425. {
  1426. unsigned ring_sizes[RADEON_NUM_RINGS];
  1427. uint32_t *ring_data[RADEON_NUM_RINGS];
  1428. bool saved = false;
  1429. int i, r;
  1430. int resched;
  1431. down_write(&rdev->exclusive_lock);
  1432. if (!rdev->needs_reset) {
  1433. up_write(&rdev->exclusive_lock);
  1434. return 0;
  1435. }
  1436. rdev->needs_reset = false;
  1437. radeon_save_bios_scratch_regs(rdev);
  1438. /* block TTM */
  1439. resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
  1440. radeon_pm_suspend(rdev);
  1441. radeon_suspend(rdev);
  1442. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1443. ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
  1444. &ring_data[i]);
  1445. if (ring_sizes[i]) {
  1446. saved = true;
  1447. dev_info(rdev->dev, "Saved %d dwords of commands "
  1448. "on ring %d.\n", ring_sizes[i], i);
  1449. }
  1450. }
  1451. retry:
  1452. r = radeon_asic_reset(rdev);
  1453. if (!r) {
  1454. dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
  1455. radeon_resume(rdev);
  1456. }
  1457. radeon_restore_bios_scratch_regs(rdev);
  1458. if (!r) {
  1459. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1460. radeon_ring_restore(rdev, &rdev->ring[i],
  1461. ring_sizes[i], ring_data[i]);
  1462. ring_sizes[i] = 0;
  1463. ring_data[i] = NULL;
  1464. }
  1465. r = radeon_ib_ring_tests(rdev);
  1466. if (r) {
  1467. dev_err(rdev->dev, "ib ring test failed (%d).\n", r);
  1468. if (saved) {
  1469. saved = false;
  1470. radeon_suspend(rdev);
  1471. goto retry;
  1472. }
  1473. }
  1474. } else {
  1475. radeon_fence_driver_force_completion(rdev);
  1476. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1477. kfree(ring_data[i]);
  1478. }
  1479. }
  1480. radeon_pm_resume(rdev);
  1481. drm_helper_resume_force_mode(rdev->ddev);
  1482. ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
  1483. if (r) {
  1484. /* bad news, how to tell it to userspace ? */
  1485. dev_info(rdev->dev, "GPU reset failed\n");
  1486. }
  1487. up_write(&rdev->exclusive_lock);
  1488. return r;
  1489. }
  1490. /*
  1491. * Debugfs
  1492. */
  1493. int radeon_debugfs_add_files(struct radeon_device *rdev,
  1494. struct drm_info_list *files,
  1495. unsigned nfiles)
  1496. {
  1497. unsigned i;
  1498. for (i = 0; i < rdev->debugfs_count; i++) {
  1499. if (rdev->debugfs[i].files == files) {
  1500. /* Already registered */
  1501. return 0;
  1502. }
  1503. }
  1504. i = rdev->debugfs_count + 1;
  1505. if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
  1506. DRM_ERROR("Reached maximum number of debugfs components.\n");
  1507. DRM_ERROR("Report so we increase "
  1508. "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
  1509. return -EINVAL;
  1510. }
  1511. rdev->debugfs[rdev->debugfs_count].files = files;
  1512. rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
  1513. rdev->debugfs_count = i;
  1514. #if defined(CONFIG_DEBUG_FS)
  1515. drm_debugfs_create_files(files, nfiles,
  1516. rdev->ddev->control->debugfs_root,
  1517. rdev->ddev->control);
  1518. drm_debugfs_create_files(files, nfiles,
  1519. rdev->ddev->primary->debugfs_root,
  1520. rdev->ddev->primary);
  1521. #endif
  1522. return 0;
  1523. }
  1524. static void radeon_debugfs_remove_files(struct radeon_device *rdev)
  1525. {
  1526. #if defined(CONFIG_DEBUG_FS)
  1527. unsigned i;
  1528. for (i = 0; i < rdev->debugfs_count; i++) {
  1529. drm_debugfs_remove_files(rdev->debugfs[i].files,
  1530. rdev->debugfs[i].num_files,
  1531. rdev->ddev->control);
  1532. drm_debugfs_remove_files(rdev->debugfs[i].files,
  1533. rdev->debugfs[i].num_files,
  1534. rdev->ddev->primary);
  1535. }
  1536. #endif
  1537. }
  1538. #if defined(CONFIG_DEBUG_FS)
  1539. int radeon_debugfs_init(struct drm_minor *minor)
  1540. {
  1541. return 0;
  1542. }
  1543. void radeon_debugfs_cleanup(struct drm_minor *minor)
  1544. {
  1545. }
  1546. #endif