cppc_acpi.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /*
  2. * CPPC (Collaborative Processor Performance Control) methods used by CPUfreq drivers.
  3. *
  4. * (C) Copyright 2014, 2015 Linaro Ltd.
  5. * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; version 2
  10. * of the License.
  11. *
  12. * CPPC describes a few methods for controlling CPU performance using
  13. * information from a per CPU table called CPC. This table is described in
  14. * the ACPI v5.0+ specification. The table consists of a list of
  15. * registers which may be memory mapped or hardware registers and also may
  16. * include some static integer values.
  17. *
  18. * CPU performance is on an abstract continuous scale as against a discretized
  19. * P-state scale which is tied to CPU frequency only. In brief, the basic
  20. * operation involves:
  21. *
  22. * - OS makes a CPU performance request. (Can provide min and max bounds)
  23. *
  24. * - Platform (such as BMC) is free to optimize request within requested bounds
  25. * depending on power/thermal budgets etc.
  26. *
  27. * - Platform conveys its decision back to OS
  28. *
  29. * The communication between OS and platform occurs through another medium
  30. * called (PCC) Platform Communication Channel. This is a generic mailbox like
  31. * mechanism which includes doorbell semantics to indicate register updates.
  32. * See drivers/mailbox/pcc.c for details on PCC.
  33. *
  34. * Finer details about the PCC and CPPC spec are available in the ACPI v5.1 and
  35. * above specifications.
  36. */
  37. #define pr_fmt(fmt) "ACPI CPPC: " fmt
  38. #include <linux/cpufreq.h>
  39. #include <linux/delay.h>
  40. #include <linux/ktime.h>
  41. #include <linux/rwsem.h>
  42. #include <linux/wait.h>
  43. #include <acpi/cppc_acpi.h>
  44. /*
  45. * Lock to provide controlled access to the PCC channel.
  46. *
  47. * For performance critical usecases(currently cppc_set_perf)
  48. * We need to take read_lock and check if channel belongs to OSPM before
  49. * reading or writing to PCC subspace
  50. * We need to take write_lock before transferring the channel ownership to
  51. * the platform via a Doorbell
  52. * This allows us to batch a number of CPPC requests if they happen to
  53. * originate in about the same time
  54. *
  55. * For non-performance critical usecases(init)
  56. * Take write_lock for all purposes which gives exclusive access
  57. */
  58. static DECLARE_RWSEM(pcc_lock);
  59. /* Indicates if there are any pending/batched PCC write commands */
  60. static bool pending_pcc_write_cmd;
  61. /* Wait queue for CPUs whose requests were batched */
  62. static DECLARE_WAIT_QUEUE_HEAD(pcc_write_wait_q);
  63. /* Used to identify if a batched request is delivered to platform */
  64. static unsigned int pcc_write_cnt;
  65. /*
  66. * The cpc_desc structure contains the ACPI register details
  67. * as described in the per CPU _CPC tables. The details
  68. * include the type of register (e.g. PCC, System IO, FFH etc.)
  69. * and destination addresses which lets us READ/WRITE CPU performance
  70. * information using the appropriate I/O methods.
  71. */
  72. static DEFINE_PER_CPU(struct cpc_desc *, cpc_desc_ptr);
  73. /* This layer handles all the PCC specifics for CPPC. */
  74. static struct mbox_chan *pcc_channel;
  75. static void __iomem *pcc_comm_addr;
  76. static int pcc_subspace_idx = -1;
  77. static bool pcc_channel_acquired;
  78. static ktime_t deadline;
  79. static unsigned int pcc_mpar, pcc_mrtt;
  80. /* pcc mapped address + header size + offset within PCC subspace */
  81. #define GET_PCC_VADDR(offs) (pcc_comm_addr + 0x8 + (offs))
  82. /* Check if a CPC regsiter is in PCC */
  83. #define CPC_IN_PCC(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
  84. (cpc)->cpc_entry.reg.space_id == \
  85. ACPI_ADR_SPACE_PLATFORM_COMM)
  86. /*
  87. * Arbitrary Retries in case the remote processor is slow to respond
  88. * to PCC commands. Keeping it high enough to cover emulators where
  89. * the processors run painfully slow.
  90. */
  91. #define NUM_RETRIES 500
  92. static int check_pcc_chan(void)
  93. {
  94. int ret = -EIO;
  95. struct acpi_pcct_shared_memory __iomem *generic_comm_base = pcc_comm_addr;
  96. ktime_t next_deadline = ktime_add(ktime_get(), deadline);
  97. /* Retry in case the remote processor was too slow to catch up. */
  98. while (!ktime_after(ktime_get(), next_deadline)) {
  99. /*
  100. * Per spec, prior to boot the PCC space wil be initialized by
  101. * platform and should have set the command completion bit when
  102. * PCC can be used by OSPM
  103. */
  104. if (readw_relaxed(&generic_comm_base->status) & PCC_CMD_COMPLETE) {
  105. ret = 0;
  106. break;
  107. }
  108. /*
  109. * Reducing the bus traffic in case this loop takes longer than
  110. * a few retries.
  111. */
  112. udelay(3);
  113. }
  114. return ret;
  115. }
  116. /*
  117. * This function transfers the ownership of the PCC to the platform
  118. * So it must be called while holding write_lock(pcc_lock)
  119. */
  120. static int send_pcc_cmd(u16 cmd)
  121. {
  122. int ret = -EIO, i;
  123. struct acpi_pcct_shared_memory *generic_comm_base =
  124. (struct acpi_pcct_shared_memory *) pcc_comm_addr;
  125. static ktime_t last_cmd_cmpl_time, last_mpar_reset;
  126. static int mpar_count;
  127. unsigned int time_delta;
  128. /*
  129. * For CMD_WRITE we know for a fact the caller should have checked
  130. * the channel before writing to PCC space
  131. */
  132. if (cmd == CMD_READ) {
  133. /*
  134. * If there are pending cpc_writes, then we stole the channel
  135. * before write completion, so first send a WRITE command to
  136. * platform
  137. */
  138. if (pending_pcc_write_cmd)
  139. send_pcc_cmd(CMD_WRITE);
  140. ret = check_pcc_chan();
  141. if (ret)
  142. goto end;
  143. } else /* CMD_WRITE */
  144. pending_pcc_write_cmd = FALSE;
  145. /*
  146. * Handle the Minimum Request Turnaround Time(MRTT)
  147. * "The minimum amount of time that OSPM must wait after the completion
  148. * of a command before issuing the next command, in microseconds"
  149. */
  150. if (pcc_mrtt) {
  151. time_delta = ktime_us_delta(ktime_get(), last_cmd_cmpl_time);
  152. if (pcc_mrtt > time_delta)
  153. udelay(pcc_mrtt - time_delta);
  154. }
  155. /*
  156. * Handle the non-zero Maximum Periodic Access Rate(MPAR)
  157. * "The maximum number of periodic requests that the subspace channel can
  158. * support, reported in commands per minute. 0 indicates no limitation."
  159. *
  160. * This parameter should be ideally zero or large enough so that it can
  161. * handle maximum number of requests that all the cores in the system can
  162. * collectively generate. If it is not, we will follow the spec and just
  163. * not send the request to the platform after hitting the MPAR limit in
  164. * any 60s window
  165. */
  166. if (pcc_mpar) {
  167. if (mpar_count == 0) {
  168. time_delta = ktime_ms_delta(ktime_get(), last_mpar_reset);
  169. if (time_delta < 60 * MSEC_PER_SEC) {
  170. pr_debug("PCC cmd not sent due to MPAR limit");
  171. ret = -EIO;
  172. goto end;
  173. }
  174. last_mpar_reset = ktime_get();
  175. mpar_count = pcc_mpar;
  176. }
  177. mpar_count--;
  178. }
  179. /* Write to the shared comm region. */
  180. writew_relaxed(cmd, &generic_comm_base->command);
  181. /* Flip CMD COMPLETE bit */
  182. writew_relaxed(0, &generic_comm_base->status);
  183. /* Ring doorbell */
  184. ret = mbox_send_message(pcc_channel, &cmd);
  185. if (ret < 0) {
  186. pr_err("Err sending PCC mbox message. cmd:%d, ret:%d\n",
  187. cmd, ret);
  188. goto end;
  189. }
  190. /*
  191. * For READs we need to ensure the cmd completed to ensure
  192. * the ensuing read()s can proceed. For WRITEs we dont care
  193. * because the actual write()s are done before coming here
  194. * and the next READ or WRITE will check if the channel
  195. * is busy/free at the entry of this call.
  196. *
  197. * If Minimum Request Turnaround Time is non-zero, we need
  198. * to record the completion time of both READ and WRITE
  199. * command for proper handling of MRTT, so we need to check
  200. * for pcc_mrtt in addition to CMD_READ
  201. */
  202. if (cmd == CMD_READ || pcc_mrtt) {
  203. ret = check_pcc_chan();
  204. if (pcc_mrtt)
  205. last_cmd_cmpl_time = ktime_get();
  206. }
  207. mbox_client_txdone(pcc_channel, ret);
  208. end:
  209. if (cmd == CMD_WRITE) {
  210. if (unlikely(ret)) {
  211. for_each_possible_cpu(i) {
  212. struct cpc_desc *desc = per_cpu(cpc_desc_ptr, i);
  213. if (!desc)
  214. continue;
  215. if (desc->write_cmd_id == pcc_write_cnt)
  216. desc->write_cmd_status = ret;
  217. }
  218. }
  219. pcc_write_cnt++;
  220. wake_up_all(&pcc_write_wait_q);
  221. }
  222. return ret;
  223. }
  224. static void cppc_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
  225. {
  226. if (ret < 0)
  227. pr_debug("TX did not complete: CMD sent:%x, ret:%d\n",
  228. *(u16 *)msg, ret);
  229. else
  230. pr_debug("TX completed. CMD sent:%x, ret:%d\n",
  231. *(u16 *)msg, ret);
  232. }
  233. struct mbox_client cppc_mbox_cl = {
  234. .tx_done = cppc_chan_tx_done,
  235. .knows_txdone = true,
  236. };
  237. static int acpi_get_psd(struct cpc_desc *cpc_ptr, acpi_handle handle)
  238. {
  239. int result = -EFAULT;
  240. acpi_status status = AE_OK;
  241. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  242. struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
  243. struct acpi_buffer state = {0, NULL};
  244. union acpi_object *psd = NULL;
  245. struct acpi_psd_package *pdomain;
  246. status = acpi_evaluate_object_typed(handle, "_PSD", NULL, &buffer,
  247. ACPI_TYPE_PACKAGE);
  248. if (ACPI_FAILURE(status))
  249. return -ENODEV;
  250. psd = buffer.pointer;
  251. if (!psd || psd->package.count != 1) {
  252. pr_debug("Invalid _PSD data\n");
  253. goto end;
  254. }
  255. pdomain = &(cpc_ptr->domain_info);
  256. state.length = sizeof(struct acpi_psd_package);
  257. state.pointer = pdomain;
  258. status = acpi_extract_package(&(psd->package.elements[0]),
  259. &format, &state);
  260. if (ACPI_FAILURE(status)) {
  261. pr_debug("Invalid _PSD data for CPU:%d\n", cpc_ptr->cpu_id);
  262. goto end;
  263. }
  264. if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
  265. pr_debug("Unknown _PSD:num_entries for CPU:%d\n", cpc_ptr->cpu_id);
  266. goto end;
  267. }
  268. if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
  269. pr_debug("Unknown _PSD:revision for CPU: %d\n", cpc_ptr->cpu_id);
  270. goto end;
  271. }
  272. if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
  273. pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
  274. pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
  275. pr_debug("Invalid _PSD:coord_type for CPU:%d\n", cpc_ptr->cpu_id);
  276. goto end;
  277. }
  278. result = 0;
  279. end:
  280. kfree(buffer.pointer);
  281. return result;
  282. }
  283. /**
  284. * acpi_get_psd_map - Map the CPUs in a common freq domain.
  285. * @all_cpu_data: Ptrs to CPU specific CPPC data including PSD info.
  286. *
  287. * Return: 0 for success or negative value for err.
  288. */
  289. int acpi_get_psd_map(struct cpudata **all_cpu_data)
  290. {
  291. int count_target;
  292. int retval = 0;
  293. unsigned int i, j;
  294. cpumask_var_t covered_cpus;
  295. struct cpudata *pr, *match_pr;
  296. struct acpi_psd_package *pdomain;
  297. struct acpi_psd_package *match_pdomain;
  298. struct cpc_desc *cpc_ptr, *match_cpc_ptr;
  299. if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
  300. return -ENOMEM;
  301. /*
  302. * Now that we have _PSD data from all CPUs, lets setup P-state
  303. * domain info.
  304. */
  305. for_each_possible_cpu(i) {
  306. pr = all_cpu_data[i];
  307. if (!pr)
  308. continue;
  309. if (cpumask_test_cpu(i, covered_cpus))
  310. continue;
  311. cpc_ptr = per_cpu(cpc_desc_ptr, i);
  312. if (!cpc_ptr) {
  313. retval = -EFAULT;
  314. goto err_ret;
  315. }
  316. pdomain = &(cpc_ptr->domain_info);
  317. cpumask_set_cpu(i, pr->shared_cpu_map);
  318. cpumask_set_cpu(i, covered_cpus);
  319. if (pdomain->num_processors <= 1)
  320. continue;
  321. /* Validate the Domain info */
  322. count_target = pdomain->num_processors;
  323. if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
  324. pr->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  325. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
  326. pr->shared_type = CPUFREQ_SHARED_TYPE_HW;
  327. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
  328. pr->shared_type = CPUFREQ_SHARED_TYPE_ANY;
  329. for_each_possible_cpu(j) {
  330. if (i == j)
  331. continue;
  332. match_cpc_ptr = per_cpu(cpc_desc_ptr, j);
  333. if (!match_cpc_ptr) {
  334. retval = -EFAULT;
  335. goto err_ret;
  336. }
  337. match_pdomain = &(match_cpc_ptr->domain_info);
  338. if (match_pdomain->domain != pdomain->domain)
  339. continue;
  340. /* Here i and j are in the same domain */
  341. if (match_pdomain->num_processors != count_target) {
  342. retval = -EFAULT;
  343. goto err_ret;
  344. }
  345. if (pdomain->coord_type != match_pdomain->coord_type) {
  346. retval = -EFAULT;
  347. goto err_ret;
  348. }
  349. cpumask_set_cpu(j, covered_cpus);
  350. cpumask_set_cpu(j, pr->shared_cpu_map);
  351. }
  352. for_each_possible_cpu(j) {
  353. if (i == j)
  354. continue;
  355. match_pr = all_cpu_data[j];
  356. if (!match_pr)
  357. continue;
  358. match_cpc_ptr = per_cpu(cpc_desc_ptr, j);
  359. if (!match_cpc_ptr) {
  360. retval = -EFAULT;
  361. goto err_ret;
  362. }
  363. match_pdomain = &(match_cpc_ptr->domain_info);
  364. if (match_pdomain->domain != pdomain->domain)
  365. continue;
  366. match_pr->shared_type = pr->shared_type;
  367. cpumask_copy(match_pr->shared_cpu_map,
  368. pr->shared_cpu_map);
  369. }
  370. }
  371. err_ret:
  372. for_each_possible_cpu(i) {
  373. pr = all_cpu_data[i];
  374. if (!pr)
  375. continue;
  376. /* Assume no coordination on any error parsing domain info */
  377. if (retval) {
  378. cpumask_clear(pr->shared_cpu_map);
  379. cpumask_set_cpu(i, pr->shared_cpu_map);
  380. pr->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  381. }
  382. }
  383. free_cpumask_var(covered_cpus);
  384. return retval;
  385. }
  386. EXPORT_SYMBOL_GPL(acpi_get_psd_map);
  387. static int register_pcc_channel(int pcc_subspace_idx)
  388. {
  389. struct acpi_pcct_hw_reduced *cppc_ss;
  390. u64 usecs_lat;
  391. if (pcc_subspace_idx >= 0) {
  392. pcc_channel = pcc_mbox_request_channel(&cppc_mbox_cl,
  393. pcc_subspace_idx);
  394. if (IS_ERR(pcc_channel)) {
  395. pr_err("Failed to find PCC communication channel\n");
  396. return -ENODEV;
  397. }
  398. /*
  399. * The PCC mailbox controller driver should
  400. * have parsed the PCCT (global table of all
  401. * PCC channels) and stored pointers to the
  402. * subspace communication region in con_priv.
  403. */
  404. cppc_ss = pcc_channel->con_priv;
  405. if (!cppc_ss) {
  406. pr_err("No PCC subspace found for CPPC\n");
  407. return -ENODEV;
  408. }
  409. /*
  410. * cppc_ss->latency is just a Nominal value. In reality
  411. * the remote processor could be much slower to reply.
  412. * So add an arbitrary amount of wait on top of Nominal.
  413. */
  414. usecs_lat = NUM_RETRIES * cppc_ss->latency;
  415. deadline = ns_to_ktime(usecs_lat * NSEC_PER_USEC);
  416. pcc_mrtt = cppc_ss->min_turnaround_time;
  417. pcc_mpar = cppc_ss->max_access_rate;
  418. pcc_comm_addr = acpi_os_ioremap(cppc_ss->base_address, cppc_ss->length);
  419. if (!pcc_comm_addr) {
  420. pr_err("Failed to ioremap PCC comm region mem\n");
  421. return -ENOMEM;
  422. }
  423. /* Set flag so that we dont come here for each CPU. */
  424. pcc_channel_acquired = true;
  425. }
  426. return 0;
  427. }
  428. /*
  429. * An example CPC table looks like the following.
  430. *
  431. * Name(_CPC, Package()
  432. * {
  433. * 17,
  434. * NumEntries
  435. * 1,
  436. * // Revision
  437. * ResourceTemplate(){Register(PCC, 32, 0, 0x120, 2)},
  438. * // Highest Performance
  439. * ResourceTemplate(){Register(PCC, 32, 0, 0x124, 2)},
  440. * // Nominal Performance
  441. * ResourceTemplate(){Register(PCC, 32, 0, 0x128, 2)},
  442. * // Lowest Nonlinear Performance
  443. * ResourceTemplate(){Register(PCC, 32, 0, 0x12C, 2)},
  444. * // Lowest Performance
  445. * ResourceTemplate(){Register(PCC, 32, 0, 0x130, 2)},
  446. * // Guaranteed Performance Register
  447. * ResourceTemplate(){Register(PCC, 32, 0, 0x110, 2)},
  448. * // Desired Performance Register
  449. * ResourceTemplate(){Register(SystemMemory, 0, 0, 0, 0)},
  450. * ..
  451. * ..
  452. * ..
  453. *
  454. * }
  455. * Each Register() encodes how to access that specific register.
  456. * e.g. a sample PCC entry has the following encoding:
  457. *
  458. * Register (
  459. * PCC,
  460. * AddressSpaceKeyword
  461. * 8,
  462. * //RegisterBitWidth
  463. * 8,
  464. * //RegisterBitOffset
  465. * 0x30,
  466. * //RegisterAddress
  467. * 9
  468. * //AccessSize (subspace ID)
  469. * 0
  470. * )
  471. * }
  472. */
  473. /**
  474. * acpi_cppc_processor_probe - Search for per CPU _CPC objects.
  475. * @pr: Ptr to acpi_processor containing this CPUs logical Id.
  476. *
  477. * Return: 0 for success or negative value for err.
  478. */
  479. int acpi_cppc_processor_probe(struct acpi_processor *pr)
  480. {
  481. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  482. union acpi_object *out_obj, *cpc_obj;
  483. struct cpc_desc *cpc_ptr;
  484. struct cpc_reg *gas_t;
  485. acpi_handle handle = pr->handle;
  486. unsigned int num_ent, i, cpc_rev;
  487. acpi_status status;
  488. int ret = -EFAULT;
  489. /* Parse the ACPI _CPC table for this cpu. */
  490. status = acpi_evaluate_object_typed(handle, "_CPC", NULL, &output,
  491. ACPI_TYPE_PACKAGE);
  492. if (ACPI_FAILURE(status)) {
  493. ret = -ENODEV;
  494. goto out_buf_free;
  495. }
  496. out_obj = (union acpi_object *) output.pointer;
  497. cpc_ptr = kzalloc(sizeof(struct cpc_desc), GFP_KERNEL);
  498. if (!cpc_ptr) {
  499. ret = -ENOMEM;
  500. goto out_buf_free;
  501. }
  502. /* First entry is NumEntries. */
  503. cpc_obj = &out_obj->package.elements[0];
  504. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  505. num_ent = cpc_obj->integer.value;
  506. } else {
  507. pr_debug("Unexpected entry type(%d) for NumEntries\n",
  508. cpc_obj->type);
  509. goto out_free;
  510. }
  511. /* Only support CPPCv2. Bail otherwise. */
  512. if (num_ent != CPPC_NUM_ENT) {
  513. pr_debug("Firmware exports %d entries. Expected: %d\n",
  514. num_ent, CPPC_NUM_ENT);
  515. goto out_free;
  516. }
  517. cpc_ptr->num_entries = num_ent;
  518. /* Second entry should be revision. */
  519. cpc_obj = &out_obj->package.elements[1];
  520. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  521. cpc_rev = cpc_obj->integer.value;
  522. } else {
  523. pr_debug("Unexpected entry type(%d) for Revision\n",
  524. cpc_obj->type);
  525. goto out_free;
  526. }
  527. if (cpc_rev != CPPC_REV) {
  528. pr_debug("Firmware exports revision:%d. Expected:%d\n",
  529. cpc_rev, CPPC_REV);
  530. goto out_free;
  531. }
  532. /* Iterate through remaining entries in _CPC */
  533. for (i = 2; i < num_ent; i++) {
  534. cpc_obj = &out_obj->package.elements[i];
  535. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  536. cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_INTEGER;
  537. cpc_ptr->cpc_regs[i-2].cpc_entry.int_value = cpc_obj->integer.value;
  538. } else if (cpc_obj->type == ACPI_TYPE_BUFFER) {
  539. gas_t = (struct cpc_reg *)
  540. cpc_obj->buffer.pointer;
  541. /*
  542. * The PCC Subspace index is encoded inside
  543. * the CPC table entries. The same PCC index
  544. * will be used for all the PCC entries,
  545. * so extract it only once.
  546. */
  547. if (gas_t->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  548. if (pcc_subspace_idx < 0)
  549. pcc_subspace_idx = gas_t->access_width;
  550. else if (pcc_subspace_idx != gas_t->access_width) {
  551. pr_debug("Mismatched PCC ids.\n");
  552. goto out_free;
  553. }
  554. } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  555. if (gas_t->address) {
  556. void __iomem *addr;
  557. addr = ioremap(gas_t->address, gas_t->bit_width/8);
  558. if (!addr)
  559. goto out_free;
  560. cpc_ptr->cpc_regs[i-2].sys_mem_vaddr = addr;
  561. }
  562. } else {
  563. /* Support only PCC and SYS MEM type regs */
  564. pr_debug("Unsupported register type: %d\n", gas_t->space_id);
  565. goto out_free;
  566. }
  567. cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_BUFFER;
  568. memcpy(&cpc_ptr->cpc_regs[i-2].cpc_entry.reg, gas_t, sizeof(*gas_t));
  569. } else {
  570. pr_debug("Err in entry:%d in CPC table of CPU:%d \n", i, pr->id);
  571. goto out_free;
  572. }
  573. }
  574. /* Store CPU Logical ID */
  575. cpc_ptr->cpu_id = pr->id;
  576. /* Parse PSD data for this CPU */
  577. ret = acpi_get_psd(cpc_ptr, handle);
  578. if (ret)
  579. goto out_free;
  580. /* Register PCC channel once for all CPUs. */
  581. if (!pcc_channel_acquired) {
  582. ret = register_pcc_channel(pcc_subspace_idx);
  583. if (ret)
  584. goto out_free;
  585. }
  586. /* Plug PSD data into this CPUs CPC descriptor. */
  587. per_cpu(cpc_desc_ptr, pr->id) = cpc_ptr;
  588. /* Everything looks okay */
  589. pr_debug("Parsed CPC struct for CPU: %d\n", pr->id);
  590. kfree(output.pointer);
  591. return 0;
  592. out_free:
  593. /* Free all the mapped sys mem areas for this CPU */
  594. for (i = 2; i < cpc_ptr->num_entries; i++) {
  595. void __iomem *addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
  596. if (addr)
  597. iounmap(addr);
  598. }
  599. kfree(cpc_ptr);
  600. out_buf_free:
  601. kfree(output.pointer);
  602. return ret;
  603. }
  604. EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe);
  605. /**
  606. * acpi_cppc_processor_exit - Cleanup CPC structs.
  607. * @pr: Ptr to acpi_processor containing this CPUs logical Id.
  608. *
  609. * Return: Void
  610. */
  611. void acpi_cppc_processor_exit(struct acpi_processor *pr)
  612. {
  613. struct cpc_desc *cpc_ptr;
  614. unsigned int i;
  615. void __iomem *addr;
  616. cpc_ptr = per_cpu(cpc_desc_ptr, pr->id);
  617. /* Free all the mapped sys mem areas for this CPU */
  618. for (i = 2; i < cpc_ptr->num_entries; i++) {
  619. addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
  620. if (addr)
  621. iounmap(addr);
  622. }
  623. kfree(cpc_ptr);
  624. }
  625. EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit);
  626. /*
  627. * Since cpc_read and cpc_write are called while holding pcc_lock, it should be
  628. * as fast as possible. We have already mapped the PCC subspace during init, so
  629. * we can directly write to it.
  630. */
  631. static int cpc_read(struct cpc_register_resource *reg_res, u64 *val)
  632. {
  633. int ret_val = 0;
  634. void __iomem *vaddr = 0;
  635. struct cpc_reg *reg = &reg_res->cpc_entry.reg;
  636. if (reg_res->type == ACPI_TYPE_INTEGER) {
  637. *val = reg_res->cpc_entry.int_value;
  638. return ret_val;
  639. }
  640. *val = 0;
  641. if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM)
  642. vaddr = GET_PCC_VADDR(reg->address);
  643. else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  644. vaddr = reg_res->sys_mem_vaddr;
  645. else
  646. return acpi_os_read_memory((acpi_physical_address)reg->address,
  647. val, reg->bit_width);
  648. switch (reg->bit_width) {
  649. case 8:
  650. *val = readb_relaxed(vaddr);
  651. break;
  652. case 16:
  653. *val = readw_relaxed(vaddr);
  654. break;
  655. case 32:
  656. *val = readl_relaxed(vaddr);
  657. break;
  658. case 64:
  659. *val = readq_relaxed(vaddr);
  660. break;
  661. default:
  662. pr_debug("Error: Cannot read %u bit width from PCC\n",
  663. reg->bit_width);
  664. ret_val = -EFAULT;
  665. }
  666. return ret_val;
  667. }
  668. static int cpc_write(struct cpc_register_resource *reg_res, u64 val)
  669. {
  670. int ret_val = 0;
  671. void __iomem *vaddr = 0;
  672. struct cpc_reg *reg = &reg_res->cpc_entry.reg;
  673. if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM)
  674. vaddr = GET_PCC_VADDR(reg->address);
  675. else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  676. vaddr = reg_res->sys_mem_vaddr;
  677. else
  678. return acpi_os_write_memory((acpi_physical_address)reg->address,
  679. val, reg->bit_width);
  680. switch (reg->bit_width) {
  681. case 8:
  682. writeb_relaxed(val, vaddr);
  683. break;
  684. case 16:
  685. writew_relaxed(val, vaddr);
  686. break;
  687. case 32:
  688. writel_relaxed(val, vaddr);
  689. break;
  690. case 64:
  691. writeq_relaxed(val, vaddr);
  692. break;
  693. default:
  694. pr_debug("Error: Cannot write %u bit width to PCC\n",
  695. reg->bit_width);
  696. ret_val = -EFAULT;
  697. break;
  698. }
  699. return ret_val;
  700. }
  701. /**
  702. * cppc_get_perf_caps - Get a CPUs performance capabilities.
  703. * @cpunum: CPU from which to get capabilities info.
  704. * @perf_caps: ptr to cppc_perf_caps. See cppc_acpi.h
  705. *
  706. * Return: 0 for success with perf_caps populated else -ERRNO.
  707. */
  708. int cppc_get_perf_caps(int cpunum, struct cppc_perf_caps *perf_caps)
  709. {
  710. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
  711. struct cpc_register_resource *highest_reg, *lowest_reg, *ref_perf,
  712. *nom_perf;
  713. u64 high, low, ref, nom;
  714. int ret = 0, regs_in_pcc = 0;
  715. if (!cpc_desc) {
  716. pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
  717. return -ENODEV;
  718. }
  719. highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF];
  720. lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF];
  721. ref_perf = &cpc_desc->cpc_regs[REFERENCE_PERF];
  722. nom_perf = &cpc_desc->cpc_regs[NOMINAL_PERF];
  723. /* Are any of the regs PCC ?*/
  724. if (CPC_IN_PCC(highest_reg) || CPC_IN_PCC(lowest_reg) ||
  725. CPC_IN_PCC(ref_perf) || CPC_IN_PCC(nom_perf)) {
  726. regs_in_pcc = 1;
  727. down_write(&pcc_lock);
  728. /* Ring doorbell once to update PCC subspace */
  729. if (send_pcc_cmd(CMD_READ) < 0) {
  730. ret = -EIO;
  731. goto out_err;
  732. }
  733. }
  734. cpc_read(highest_reg, &high);
  735. perf_caps->highest_perf = high;
  736. cpc_read(lowest_reg, &low);
  737. perf_caps->lowest_perf = low;
  738. cpc_read(ref_perf, &ref);
  739. perf_caps->reference_perf = ref;
  740. cpc_read(nom_perf, &nom);
  741. perf_caps->nominal_perf = nom;
  742. if (!ref)
  743. perf_caps->reference_perf = perf_caps->nominal_perf;
  744. if (!high || !low || !nom)
  745. ret = -EFAULT;
  746. out_err:
  747. if (regs_in_pcc)
  748. up_write(&pcc_lock);
  749. return ret;
  750. }
  751. EXPORT_SYMBOL_GPL(cppc_get_perf_caps);
  752. /**
  753. * cppc_get_perf_ctrs - Read a CPUs performance feedback counters.
  754. * @cpunum: CPU from which to read counters.
  755. * @perf_fb_ctrs: ptr to cppc_perf_fb_ctrs. See cppc_acpi.h
  756. *
  757. * Return: 0 for success with perf_fb_ctrs populated else -ERRNO.
  758. */
  759. int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs)
  760. {
  761. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
  762. struct cpc_register_resource *delivered_reg, *reference_reg;
  763. u64 delivered, reference;
  764. int ret = 0, regs_in_pcc = 0;
  765. if (!cpc_desc) {
  766. pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
  767. return -ENODEV;
  768. }
  769. delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR];
  770. reference_reg = &cpc_desc->cpc_regs[REFERENCE_CTR];
  771. /* Are any of the regs PCC ?*/
  772. if (CPC_IN_PCC(delivered_reg) || CPC_IN_PCC(reference_reg)) {
  773. down_write(&pcc_lock);
  774. regs_in_pcc = 1;
  775. /* Ring doorbell once to update PCC subspace */
  776. if (send_pcc_cmd(CMD_READ) < 0) {
  777. ret = -EIO;
  778. goto out_err;
  779. }
  780. }
  781. cpc_read(delivered_reg, &delivered);
  782. cpc_read(reference_reg, &reference);
  783. if (!delivered || !reference) {
  784. ret = -EFAULT;
  785. goto out_err;
  786. }
  787. perf_fb_ctrs->delivered = delivered;
  788. perf_fb_ctrs->reference = reference;
  789. perf_fb_ctrs->delivered -= perf_fb_ctrs->prev_delivered;
  790. perf_fb_ctrs->reference -= perf_fb_ctrs->prev_reference;
  791. perf_fb_ctrs->prev_delivered = delivered;
  792. perf_fb_ctrs->prev_reference = reference;
  793. out_err:
  794. if (regs_in_pcc)
  795. up_write(&pcc_lock);
  796. return ret;
  797. }
  798. EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs);
  799. /**
  800. * cppc_set_perf - Set a CPUs performance controls.
  801. * @cpu: CPU for which to set performance controls.
  802. * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h
  803. *
  804. * Return: 0 for success, -ERRNO otherwise.
  805. */
  806. int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)
  807. {
  808. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
  809. struct cpc_register_resource *desired_reg;
  810. int ret = 0;
  811. if (!cpc_desc) {
  812. pr_debug("No CPC descriptor for CPU:%d\n", cpu);
  813. return -ENODEV;
  814. }
  815. desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
  816. /*
  817. * This is Phase-I where we want to write to CPC registers
  818. * -> We want all CPUs to be able to execute this phase in parallel
  819. *
  820. * Since read_lock can be acquired by multiple CPUs simultaneously we
  821. * achieve that goal here
  822. */
  823. if (CPC_IN_PCC(desired_reg)) {
  824. down_read(&pcc_lock); /* BEGIN Phase-I */
  825. /*
  826. * If there are pending write commands i.e pending_pcc_write_cmd
  827. * is TRUE, then we know OSPM owns the channel as another CPU
  828. * has already checked for command completion bit and updated
  829. * the corresponding CPC registers
  830. */
  831. if (!pending_pcc_write_cmd) {
  832. ret = check_pcc_chan();
  833. if (ret) {
  834. up_read(&pcc_lock);
  835. return ret;
  836. }
  837. /*
  838. * Update the pending_write to make sure a PCC CMD_READ
  839. * will not arrive and steal the channel during the
  840. * transition to write lock
  841. */
  842. pending_pcc_write_cmd = TRUE;
  843. }
  844. cpc_desc->write_cmd_id = pcc_write_cnt;
  845. cpc_desc->write_cmd_status = 0;
  846. }
  847. /*
  848. * Skip writing MIN/MAX until Linux knows how to come up with
  849. * useful values.
  850. */
  851. cpc_write(desired_reg, perf_ctrls->desired_perf);
  852. if (CPC_IN_PCC(desired_reg))
  853. up_read(&pcc_lock); /* END Phase-I */
  854. /*
  855. * This is Phase-II where we transfer the ownership of PCC to Platform
  856. *
  857. * Short Summary: Basically if we think of a group of cppc_set_perf
  858. * requests that happened in short overlapping interval. The last CPU to
  859. * come out of Phase-I will enter Phase-II and ring the doorbell.
  860. *
  861. * We have the following requirements for Phase-II:
  862. * 1. We want to execute Phase-II only when there are no CPUs
  863. * currently executing in Phase-I
  864. * 2. Once we start Phase-II we want to avoid all other CPUs from
  865. * entering Phase-I.
  866. * 3. We want only one CPU among all those who went through Phase-I
  867. * to run phase-II
  868. *
  869. * If write_trylock fails to get the lock and doesn't transfer the
  870. * PCC ownership to the platform, then one of the following will be TRUE
  871. * 1. There is at-least one CPU in Phase-I which will later execute
  872. * write_trylock, so the CPUs in Phase-I will be responsible for
  873. * executing the Phase-II.
  874. * 2. Some other CPU has beaten this CPU to successfully execute the
  875. * write_trylock and has already acquired the write_lock. We know for a
  876. * fact it(other CPU acquiring the write_lock) couldn't have happened
  877. * before this CPU's Phase-I as we held the read_lock.
  878. * 3. Some other CPU executing pcc CMD_READ has stolen the
  879. * down_write, in which case, send_pcc_cmd will check for pending
  880. * CMD_WRITE commands by checking the pending_pcc_write_cmd.
  881. * So this CPU can be certain that its request will be delivered
  882. * So in all cases, this CPU knows that its request will be delivered
  883. * by another CPU and can return
  884. *
  885. * After getting the down_write we still need to check for
  886. * pending_pcc_write_cmd to take care of the following scenario
  887. * The thread running this code could be scheduled out between
  888. * Phase-I and Phase-II. Before it is scheduled back on, another CPU
  889. * could have delivered the request to Platform by triggering the
  890. * doorbell and transferred the ownership of PCC to platform. So this
  891. * avoids triggering an unnecessary doorbell and more importantly before
  892. * triggering the doorbell it makes sure that the PCC channel ownership
  893. * is still with OSPM.
  894. * pending_pcc_write_cmd can also be cleared by a different CPU, if
  895. * there was a pcc CMD_READ waiting on down_write and it steals the lock
  896. * before the pcc CMD_WRITE is completed. pcc_send_cmd checks for this
  897. * case during a CMD_READ and if there are pending writes it delivers
  898. * the write command before servicing the read command
  899. */
  900. if (CPC_IN_PCC(desired_reg)) {
  901. if (down_write_trylock(&pcc_lock)) { /* BEGIN Phase-II */
  902. /* Update only if there are pending write commands */
  903. if (pending_pcc_write_cmd)
  904. send_pcc_cmd(CMD_WRITE);
  905. up_write(&pcc_lock); /* END Phase-II */
  906. } else
  907. /* Wait until pcc_write_cnt is updated by send_pcc_cmd */
  908. wait_event(pcc_write_wait_q,
  909. cpc_desc->write_cmd_id != pcc_write_cnt);
  910. /* send_pcc_cmd updates the status in case of failure */
  911. ret = cpc_desc->write_cmd_status;
  912. }
  913. return ret;
  914. }
  915. EXPORT_SYMBOL_GPL(cppc_set_perf);