summaryrefslogtreecommitdiff
path: root/acpica/acpi_init.c
blob: 585aafe6e2804f83940fbaed9581bddfdea7f7cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include "acpi_init.h"
#include <kern/timer.h>		// timer
#include <kern/printf.h>	// vsnprintf
#include <kern/assert.h>
#include <vm/vm_kern.h>		// kmem_alloc_wired 
#include <vm/pmap.h>		// hack
#include <i386/pio.h>
#include <stddef.h>
#define MACH_INCLUDE
#include <linux/delay.h>	// udelay

#define ACPI_MAX_TABLES 128

// Lets keep the ACPI tables in this module
static struct acpi_table_desc initial_tables[ACPI_MAX_TABLES];


void *
pmap_hack (unsigned long offset, unsigned long size)
{
  vm_offset_t addr;
  kern_return_t ret;
  
  ret = kmem_alloc_wired (kernel_map, &addr, round_page (size));
  if (ret != KERN_SUCCESS)
    return NULL;

  (void) pmap_map_bd (addr, offset, offset + round_page (size),
                      VM_PROT_READ | VM_PROT_WRITE);

  /* XXX remember mapping somewhere so we can free it? */

  return (void *) addr; 
}

int
islower (char c)
{
	return ((c <= 'z') && (c >= 'a'));
}

int
isupper (char c)
{
	return ((c <= 'Z') && (c >= 'A'));
}

int
isspace (char c)
{
	return ((c == '\t') || (c == ' ') || (c == '\n')); 
}

int
isdigit (char c)
{
	return ((c <= '9') && (c >= '0'));
}

int
isxdigit (char c)
{
	return ( ((c <= 'F') && (c >= 'A')) ||
		 ((c <= 'f') && (c >= 'a')) ||
		 isdigit(c) );
}

char
tolower(char c)
{
        if (isupper(c))
                c -= 'A'-'a';
        return c;
}

char
toupper(char c)
{
        if (islower(c))
                c -= 'a'-'A';
        return c;
}

int
isprint(char c)
{
	return ((c <= 126) && (c >= 32));
}



inline char *
strncat(char * dest,const char * src,size_t count)
{
int d0, d1, d2, d3;
__asm__ __volatile__(
        "cld\n\t"
        "repne\n\t"
        "scasb\n\t"
        "decl %1\n\t"
        "movl %8,%3\n"
        "1:\tdecl %3\n\t"
        "js 2f\n\t"
        "lodsb\n\t"
        "stosb\n\t"
        "testb %%al,%%al\n\t"
        "jne 1b\n"
        "2:\txorl %2,%2\n\t"
        "stosb"
        : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
        : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
        : "memory");
return dest;
}

inline char *
strcat(char * dest,const char * src)
{
int d0, d1, d2, d3;
__asm__ __volatile__(
        "cld\n\t"
        "repne\n\t"
        "scasb\n\t"
        "decl %1\n"
        "1:\tlodsb\n\t"
        "stosb\n\t"
        "testb %%al,%%al\n\t"
        "jne 1b"
        : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
        : "0" (src), "1" (dest), "2" (0), "3" (0xffffffff):"memory");
return dest;
}

void
acpi_ds_dump_method_stack(acpi_status status, ...)
//			  struct acpi_walk_state *walk_state,
//			  union acpi_parse_object *op)
{
	return;
}

acpi_status
acpi_os_create_cache(char *cache_name,
		     u16 object_size,
		     u16 max_depth, acpi_cache_t **return_cache)
{
	kmem_cache_init (*return_cache, cache_name, object_size, 0, NULL, 0);
	return (AE_OK);
}

acpi_status
acpi_os_delete_cache(acpi_cache_t *cache)
{
	slab_collect ();
	return (AE_OK);
}

acpi_status
acpi_os_purge_cache(acpi_cache_t *cache)
{
	return (AE_OK);
}

acpi_status
acpi_os_release_object(acpi_cache_t *cache, void *object)
{
	kmem_cache_free (cache, (vm_offset_t)object);
	return (AE_OK);
}

void
acpi_os_printf(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	acpi_os_vprintf(fmt, args);
	va_end(args);
}

void
acpi_os_vprintf(const char *fmt, va_list args)
{
	static char buffer[512];

	vsnprintf(buffer, 512, fmt, args);
	printf(buffer);
}

acpi_cpu_flags
acpi_os_acquire_lock(acpi_spinlock lockp)
{
	simple_lock(lockp);
	return 0;
}

void
acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
{
	simple_unlock(lockp);
}

void
acpi_os_delete_lock(acpi_spinlock handle)
{
	ACPI_FREE(handle);
}

/* FIXME: Need proper semaphores */
acpi_status
acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_semaphore * handle)
{
	acpi_spinlock *lockp = (acpi_spinlock *)handle;

	assert (max_units <= 1);
	assert (initial_units <= 1);

	acpi_os_create_lock (lockp);

	if (initial_units == 1)
		acpi_os_acquire_lock (*lockp);

	return AE_OK;
}

acpi_status
acpi_os_delete_semaphore(acpi_semaphore handle)
{
	acpi_os_delete_lock ((acpi_spinlock)handle);
	return AE_OK;
}

acpi_status
acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
{
	return AE_OK;
}

acpi_status
acpi_os_signal_semaphore(acpi_handle handle, u32 units)
{
	assert (units <= 1);
	if (units == 1)
		acpi_os_release_lock ((acpi_spinlock)handle, 0);
	return AE_OK;
}

acpi_status
acpi_os_execute(acpi_execute_type type,
		acpi_osd_exec_callback function, void *context)
{
	acpi_os_printf("XXX acpi_os_execute: not implemented\n");
	return (AE_OK);
}

acpi_physical_address
acpi_os_get_root_pointer(void)
{
	acpi_physical_address pa;

	acpi_find_root_pointer(&pa);
	return pa;
}

/* XXX: Supposed to be 64 bit free-running
 * monotonic timer with 100ns granularity
 * but we just pretend for now
 */
u64
acpi_os_get_timer(void)
{
	time_value_t now;

	timer_read(current_timer[0], &now);
	return (u64)(now.microseconds * 10);
}

void *
acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
{
	uintptr_t into_page = phys % PAGE_SIZE;
	uintptr_t nearest_page = (uintptr_t)trunc_page(phys);
	void *ret = pmap_hack (nearest_page, size + into_page);
	if (!ret) {
		acpi_os_printf("Can't map memory 0x%llx\n", phys);
	}
	return ret + into_page;
}

acpi_status
acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
{
	u32 dummy;

	if (!value)
		value = &dummy;

	*value = 0;
	if (width <= 8) {
		*(u8 *) value = inb(port);
	} else if (width <= 16) {
		*(u16 *) value = inw(port);
	} else if (width <= 32) {
		*(u32 *) value = inl(port);
	} else {
		acpi_os_printf("ACPI: read port invalid width\n");
	}

	return AE_OK;
}

acpi_status
acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
			    acpi_string *new_val)
{
	if (!init_val || !new_val)
		return AE_BAD_PARAMETER;

	return AE_OK;
}

acpi_status
acpi_os_signal(u32 function, void *info)
{
	switch (function) {
	case ACPI_SIGNAL_FATAL:
		acpi_os_printf("ACPI: Fatal opcode executed\n");
		break;
	case ACPI_SIGNAL_BREAKPOINT:
		break;
	default:
		break;
	}
	return AE_OK;
}

acpi_status
acpi_os_physical_table_override(struct acpi_table_header *existing_table,
				acpi_physical_address *address,
				u32 *table_length)
{
	*table_length = 0;
	*address = 0;
	return AE_OK;
}

acpi_status
acpi_os_table_override(struct acpi_table_header *existing_table,
		       struct acpi_table_header **new_table)
{
	if (!existing_table || !new_table)
		return AE_BAD_PARAMETER;

	*new_table = NULL;
	return AE_OK;
}

void
acpi_os_unmap_memory(void *virt, acpi_size size)
{
	void *freeme = (void *)trunc_page(virt);
	if (!freeme) {
		acpi_os_printf("Nothing to unmap\n");
		return;
	}
	//FIXME crashes: vfree (freeme);
}

static acpi_status
acpi_os_read_iomem(void *virt_addr, u64 *value, u32 width)
{

	switch (width) {
	case 8:
		*(u8 *) value = *((volatile u8 *)virt_addr);
		break;
	case 16:
		*(u16 *) value = *((volatile u16 *)virt_addr);
		break;
	case 32:
		*(u32 *) value = *((volatile u32 *)virt_addr);
		break;
	case 64:
		*(u64 *) value = *((volatile u64 *)virt_addr);
		break;
	default:
		return AE_ERROR;
	}
	return AE_OK;
}

acpi_status
acpi_os_read_memory(acpi_physical_address phys_addr, u64 *value, u32 width)
{
	void *virt_addr;
	unsigned int size = width / 8;
	u64 dummy;
	acpi_status err = AE_BAD_ADDRESS;

	virt_addr = acpi_os_map_memory (phys_addr, size);
	if (!virt_addr)
		return err;

	if (!value)
		value = &dummy;

	if (!acpi_os_read_iomem (virt_addr, value, width))
		err = AE_OK;

	acpi_os_unmap_memory (virt_addr, size);

	return err;
}

void
acpi_os_sleep(u64 ms)
{
	udelay(1000 * ms);
}

void
acpi_os_stall(u32 us)
{
	udelay(us);
}

void acpi_os_wait_events_complete(void)
{
	return;
}

acpi_status
acpi_os_write_memory(acpi_physical_address phys_addr, u64 value, u32 width)
{
	void *virt_addr;
	unsigned int size = width / 8;
	acpi_status err = AE_OK;

	virt_addr = acpi_os_map_memory (phys_addr, size);
	if (!virt_addr)
		return AE_BAD_ADDRESS;

	switch (width) {
	case 8:
		*((volatile u8 *)virt_addr) = (u8)value;
		break;
	case 16:
		*((volatile u16 *)virt_addr) = (u16)value;
		break;
	case 32:
		*((volatile u32 *)virt_addr) = (u32)value;
		break;
	case 64:
		*((volatile u64 *)virt_addr) = value;
		break;
	default:
		acpi_os_printf("ACPI: Bad write memory width\n");
		err = AE_BAD_ADDRESS;
		break;
	}

	acpi_os_unmap_memory (virt_addr, size);

	return err;
}

acpi_status
acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
{
	if (width <= 8) {
		outb(port, value);
	} else if (width <= 16) {
		outw(port, value);
	} else if (width <= 32) {
		outl(port, value);
	} else {
		acpi_os_printf("ACPI: Bad write port width\n");
	}

	return AE_OK;
}


acpi_status
acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
				  void *context)
{
	return AE_OK;
}

acpi_status
acpi_os_remove_interrupt_handler(u32 gsi, acpi_osd_handler handler)
{
	return AE_OK;
}

acpi_status
acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
			       u64 *value, u32 width)
{
	acpi_os_printf("ACPI: Tried to read pci config\n");
	return AE_ERROR;
}

acpi_status
acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
				u64 value, u32 width)
{
	acpi_os_printf("ACPI: Tried to write pci config\n");
	return AE_ERROR;
}

acpi_status
acpi_os_initialize(void)
{
	return AE_OK;
}

acpi_status
acpi_os_terminate(void)
{
	acpi_os_printf("Bye!\n");
	return AE_OK;
}

/*
 * Missing symbols to implement:
 *
 * x isdigit
 * x isprint
 * x isspace
 * x isxdigit
 * x tolower
 * x toupper
 * x acpi_ds_dump_method_stack
 * x acpi_os_acquire_lock
 * x acpi_os_create_cache
 * x acpi_os_create_semaphore
 * x acpi_os_delete_cache
 * x acpi_os_delete_lock
 * x acpi_os_delete_semaphore
 * x acpi_os_get_root_pointer
 * x acpi_os_get_timer
 * x acpi_os_map_memory
 * x acpi_os_physical_table_override
 * x acpi_os_predefined_override
 * x acpi_os_printf
 * x acpi_os_read_memory
 * x acpi_os_read_port
 * x acpi_os_release_lock
 * x acpi_os_release_object
 * x acpi_os_signal
 * x acpi_os_signal_semaphore
 * x acpi_os_sleep
 * x acpi_os_stall
 * x acpi_os_table_override
 * x acpi_os_unmap_memory
 * x acpi_os_vprintf
 * x acpi_os_wait_events_complete
 * x acpi_os_wait_semaphore
 * x acpi_os_write_memory
 * x acpi_os_write_port
 * xx acpi_os_execute
 * xx acpi_os_install_interrupt_handler
 * xx acpi_os_read_pci_configuration
 * xx acpi_os_remove_interrupt_handler
 * xx acpi_os_write_pci_configuration
 * x acpi_os_initialize
 * x acpi_os_purge_cache
 * x acpi_os_terminate
 */

void acpi_init(void)
{
	int where = 0;
	acpi_status err;

	// Hack to increase verbosity except parsing AML
	//acpi_dbg_level = (ACPI_LV_ALL) & ~(ACPI_LV_FUNCTIONS);

	err = acpi_initialize_tables (initial_tables, ACPI_MAX_TABLES, 0);
	if (ACPI_FAILURE (err)) {
		where = 1;
		goto die;
	}

	err = acpi_initialize_subsystem ();
	if (ACPI_FAILURE (err)) {
		where = 2;
		goto die;
	}

	err = acpi_reallocate_root_table ();
	if (ACPI_FAILURE (err)) {
		where = 3;
		goto die;
	}

	err = acpi_load_tables ();
	if (ACPI_FAILURE (err)) {
		where = 4;
		goto die;
	}

	err = acpi_enable_subsystem (ACPI_FULL_INITIALIZATION);
	if (ACPI_FAILURE (err)) {
		where = 5;
		goto die;
	}

	err = acpi_initialize_objects (ACPI_FULL_INITIALIZATION);
	if (ACPI_FAILURE (err)) {
		where = 6;
		goto die;
	}

	acpi_os_printf("PASS! (wait here)\n");
	for (;;);
die:
	acpi_os_printf("OUCH! where = %d\n", where);
	for (;;);
}