minor corrections by M.Taguchi
[reactos.git] / reactos / drivers / bus / acpi / executer / amregion.c
1
2 /******************************************************************************
3 *
4 * Module Name: amregion - ACPI default Op_region (address space) handlers
5 * $Revision: 1.1 $
6 *
7 *****************************************************************************/
8
9 /*
10 * Copyright (C) 2000, 2001 R. Byron Moore
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27
28 #include "acpi.h"
29 #include "acinterp.h"
30 #include "amlcode.h"
31 #include "acnamesp.h"
32 #include "achware.h"
33 #include "acevents.h"
34
35
36 #define _COMPONENT ACPI_EXECUTER
37 MODULE_NAME ("amregion")
38
39
40 /*******************************************************************************
41 *
42 * FUNCTION: Acpi_aml_system_memory_space_handler
43 *
44 * PARAMETERS: Function - Read or Write operation
45 * Address - Where in the space to read or write
46 * Bit_width - Field width in bits (8, 16, or 32)
47 * Value - Pointer to in or out value
48 * Handler_context - Pointer to Handler's context
49 * Region_context - Pointer to context specific to the
50 * accessed region
51 *
52 * RETURN: Status
53 *
54 * DESCRIPTION: Handler for the System Memory address space (Op Region)
55 *
56 ******************************************************************************/
57
58 ACPI_STATUS
59 acpi_aml_system_memory_space_handler (
60 u32 function,
61 ACPI_PHYSICAL_ADDRESS address,
62 u32 bit_width,
63 u32 *value,
64 void *handler_context,
65 void *region_context)
66 {
67 ACPI_STATUS status = AE_OK;
68 void *logical_addr_ptr = NULL;
69 MEM_HANDLER_CONTEXT *mem_info = region_context;
70 u32 length;
71
72
73 /* Validate and translate the bit width */
74
75 switch (bit_width) {
76 case 8:
77 length = 1;
78 break;
79
80 case 16:
81 length = 2;
82 break;
83
84 case 32:
85 length = 4;
86 break;
87
88 default:
89 return (AE_AML_OPERAND_VALUE);
90 break;
91 }
92
93
94 /*
95 * Does the request fit into the cached memory mapping?
96 * Is 1) Address below the current mapping? OR
97 * 2) Address beyond the current mapping?
98 */
99
100 if ((address < mem_info->mapped_physical_address) ||
101 (((ACPI_INTEGER) address + length) >
102 ((ACPI_INTEGER) mem_info->mapped_physical_address + mem_info->mapped_length))) {
103 /*
104 * The request cannot be resolved by the current memory mapping;
105 * Delete the existing mapping and create a new one.
106 */
107
108 if (mem_info->mapped_length) {
109 /* Valid mapping, delete it */
110
111 acpi_os_unmap_memory (mem_info->mapped_logical_address,
112 mem_info->mapped_length);
113 }
114
115 mem_info->mapped_length = 0; /* In case of failure below */
116
117 /* Create a new mapping starting at the address given */
118
119 status = acpi_os_map_memory (address, SYSMEM_REGION_WINDOW_SIZE,
120 (void **) &mem_info->mapped_logical_address);
121 if (ACPI_FAILURE (status)) {
122 return (status);
123 }
124
125 /* TBD: should these pointers go to 64-bit in all cases ? */
126
127 mem_info->mapped_physical_address = address;
128 mem_info->mapped_length = SYSMEM_REGION_WINDOW_SIZE;
129 }
130
131
132 /*
133 * Generate a logical pointer corresponding to the address we want to
134 * access
135 */
136
137 /* TBD: should these pointers go to 64-bit in all cases ? */
138
139 logical_addr_ptr = mem_info->mapped_logical_address +
140 ((ACPI_INTEGER) address - (ACPI_INTEGER) mem_info->mapped_physical_address);
141
142 /* Perform the memory read or write */
143
144 switch (function) {
145
146 case ADDRESS_SPACE_READ:
147
148 switch (bit_width) {
149 case 8:
150 *value = (u32)* (u8 *) logical_addr_ptr;
151 break;
152
153 case 16:
154 MOVE_UNALIGNED16_TO_32 (value, logical_addr_ptr);
155 break;
156
157 case 32:
158 MOVE_UNALIGNED32_TO_32 (value, logical_addr_ptr);
159 break;
160 }
161
162 break;
163
164
165 case ADDRESS_SPACE_WRITE:
166
167 switch (bit_width) {
168 case 8:
169 *(u8 *) logical_addr_ptr = (u8) *value;
170 break;
171
172 case 16:
173 MOVE_UNALIGNED16_TO_16 (logical_addr_ptr, value);
174 break;
175
176 case 32:
177 MOVE_UNALIGNED32_TO_32 (logical_addr_ptr, value);
178 break;
179 }
180
181 break;
182
183
184 default:
185 status = AE_BAD_PARAMETER;
186 break;
187 }
188
189 return (status);
190 }
191
192
193 /*******************************************************************************
194 *
195 * FUNCTION: Acpi_aml_system_io_space_handler
196 *
197 * PARAMETERS: Function - Read or Write operation
198 * Address - Where in the space to read or write
199 * Bit_width - Field width in bits (8, 16, or 32)
200 * Value - Pointer to in or out value
201 * Handler_context - Pointer to Handler's context
202 * Region_context - Pointer to context specific to the
203 * accessed region
204 *
205 * RETURN: Status
206 *
207 * DESCRIPTION: Handler for the System IO address space (Op Region)
208 *
209 ******************************************************************************/
210
211 ACPI_STATUS
212 acpi_aml_system_io_space_handler (
213 u32 function,
214 ACPI_PHYSICAL_ADDRESS address,
215 u32 bit_width,
216 u32 *value,
217 void *handler_context,
218 void *region_context)
219 {
220 ACPI_STATUS status = AE_OK;
221
222
223 /* Decode the function parameter */
224
225 switch (function) {
226
227 case ADDRESS_SPACE_READ:
228
229 switch (bit_width) {
230 /* I/O Port width */
231
232 case 8:
233 *value = (u32) acpi_os_in8 ((ACPI_IO_ADDRESS) address);
234 break;
235
236 case 16:
237 *value = (u32) acpi_os_in16 ((ACPI_IO_ADDRESS) address);
238 break;
239
240 case 32:
241 *value = acpi_os_in32 ((ACPI_IO_ADDRESS) address);
242 break;
243
244 default:
245 status = AE_AML_OPERAND_VALUE;
246 }
247
248 break;
249
250
251 case ADDRESS_SPACE_WRITE:
252
253 switch (bit_width) {
254 /* I/O Port width */
255 case 8:
256 acpi_os_out8 ((ACPI_IO_ADDRESS) address, (u8) *value);
257 break;
258
259 case 16:
260 acpi_os_out16 ((ACPI_IO_ADDRESS) address, (u16) *value);
261 break;
262
263 case 32:
264 acpi_os_out32 ((ACPI_IO_ADDRESS) address, *value);
265 break;
266
267 default:
268 status = AE_AML_OPERAND_VALUE;
269 }
270
271 break;
272
273
274 default:
275 status = AE_BAD_PARAMETER;
276 break;
277 }
278
279 return (status);
280 }
281
282 /*******************************************************************************
283 *
284 * FUNCTION: Acpi_aml_pci_config_space_handler
285 *
286 * PARAMETERS: Function - Read or Write operation
287 * Address - Where in the space to read or write
288 * Bit_width - Field width in bits (8, 16, or 32)
289 * Value - Pointer to in or out value
290 * Handler_context - Pointer to Handler's context
291 * Region_context - Pointer to context specific to the
292 * accessed region
293 *
294 * RETURN: Status
295 *
296 * DESCRIPTION: Handler for the PCI Config address space (Op Region)
297 *
298 ******************************************************************************/
299
300 ACPI_STATUS
301 acpi_aml_pci_config_space_handler (
302 u32 function,
303 ACPI_PHYSICAL_ADDRESS address,
304 u32 bit_width,
305 u32 *value,
306 void *handler_context,
307 void *region_context)
308 {
309 ACPI_STATUS status = AE_OK;
310 u32 pci_bus;
311 u32 dev_func;
312 u8 pci_reg;
313 PCI_HANDLER_CONTEXT *PCIcontext;
314
315
316 /*
317 * The arguments to Acpi_os(Read|Write)Pci_cfg(Byte|Word|Dword) are:
318 *
319 * Seg_bus - 0xSSSSBBBB - SSSS is the PCI bus segment
320 * BBBB is the PCI bus number
321 *
322 * Dev_func - 0xDDDDFFFF - DDDD is the PCI device number
323 * FFFF is the PCI device function number
324 *
325 * Reg_num - Config space register must be < 40h
326 *
327 * Value - input value for write, output for read
328 *
329 */
330
331 PCIcontext = (PCI_HANDLER_CONTEXT *) region_context;
332
333 pci_bus = LOWORD(PCIcontext->seg) << 16;
334 pci_bus |= LOWORD(PCIcontext->bus);
335
336 dev_func = PCIcontext->dev_func;
337
338 pci_reg = (u8) address;
339
340 switch (function) {
341
342 case ADDRESS_SPACE_READ:
343
344 *value = 0;
345
346 switch (bit_width) {
347 /* PCI Register width */
348
349 case 8:
350 status = acpi_os_read_pci_cfg_byte (pci_bus, dev_func, pci_reg,
351 (u8 *) value);
352 break;
353
354 case 16:
355 status = acpi_os_read_pci_cfg_word (pci_bus, dev_func, pci_reg,
356 (u16 *) value);
357 break;
358
359 case 32:
360 status = acpi_os_read_pci_cfg_dword (pci_bus, dev_func, pci_reg,
361 value);
362 break;
363
364 default:
365 status = AE_AML_OPERAND_VALUE;
366
367 } /* Switch bit_width */
368
369 break;
370
371
372 case ADDRESS_SPACE_WRITE:
373
374 switch (bit_width) {
375 /* PCI Register width */
376
377 case 8:
378 status = acpi_os_write_pci_cfg_byte (pci_bus, dev_func, pci_reg,
379 *(u8 *) value);
380 break;
381
382 case 16:
383 status = acpi_os_write_pci_cfg_word (pci_bus, dev_func, pci_reg,
384 *(u16 *) value);
385 break;
386
387 case 32:
388 status = acpi_os_write_pci_cfg_dword (pci_bus, dev_func, pci_reg,
389 *value);
390 break;
391
392 default:
393 status = AE_AML_OPERAND_VALUE;
394
395 } /* Switch bit_width */
396
397 break;
398
399
400 default:
401
402 status = AE_BAD_PARAMETER;
403 break;
404
405 }
406
407 return (status);
408 }
409