Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
/*
 *  linux/arch/arm/mach-sa1100/pci-sa1111.c
 *
 *  Special pci_{map/unmap/dma_sync}_* routines for SA-1111.
 *
 *  These functions utilize bouncer buffers to compensate for a bug in
 *  the SA-1111 hardware which don't allow DMA to/from addresses
 *  certain addresses above 1MB.
 *
 *  Re-written by Christopher Hoover <ch@murgatroid.com>
 *  Original version by Brad Parker (brad@heeltoe.com)
 *
 *  Copyright (C) 2002 Hewlett Packard Company.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  version 2 as published by the Free Software Foundation.
 * */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/pci.h>
#include <linux/list.h>
#include <asm/hardware/sa1111.h>

//#define DEBUG
#ifdef DEBUG
#define DPRINTK(...) do { printk(KERN_DEBUG __VA_ARGS__); } while (0)
#else
#define DPRINTK(...) do { } while (0)
#endif

//#define STATS
#ifdef STATS
#define DO_STATS(X) do { X ; } while (0)
#else
#define DO_STATS(X) do { } while (0)
#endif

/* ************************************************** */

struct safe_buffer {
	struct list_head node;

	/* original request */
	void		*ptr;
	size_t		size;
	int		direction;

	/* safe buffer info */
	struct pci_pool *pool;
	void		*safe;
	dma_addr_t	safe_dma_addr;
};

static LIST_HEAD(safe_buffers);


#define SIZE_SMALL	1024
#define SIZE_LARGE	(4*1024)

static struct pci_pool *small_buffer_pool, *large_buffer_pool;

#ifdef STATS
static unsigned long sbp_allocs __initdata = 0;
static unsigned long lbp_allocs __initdata = 0;
static unsigned long total_allocs __initdata= 0;

static void print_alloc_stats(void)
{
	printk(KERN_INFO
	       "sa1111_pcibuf: sbp: %lu, lbp: %lu, other: %lu, total: %lu\n",
	       sbp_allocs, lbp_allocs,
	       total_allocs - sbp_allocs - lbp_allocs, total_allocs);
}
#endif

static int __init
create_safe_buffer_pools(void)
{
	small_buffer_pool = pci_pool_create("sa1111_small_dma_buffer",
					    SA1111_FAKE_PCIDEV,
					    SIZE_SMALL,
					    0 /* byte alignment */,
					    0 /* no page-crossing issues */);
	if (0 == small_buffer_pool) {
		printk(KERN_ERR
		       "sa1111_pcibuf: could not allocate small pci pool\n");
		return -1;
	}

	large_buffer_pool = pci_pool_create("sa1111_large_dma_buffer",
					    SA1111_FAKE_PCIDEV,
					    SIZE_LARGE,
					    0 /* byte alignment */,
					    0 /* no page-crossing issues */);
	if (0 == large_buffer_pool) {
		printk(KERN_ERR
		       "sa1111_pcibuf: could not allocate large pci pool\n");
		pci_pool_destroy(small_buffer_pool);
		small_buffer_pool = 0;
		return -1;
	}

	printk(KERN_INFO
	       "sa1111_pcibuf: buffer sizes: small=%u, large=%u\n",
	       SIZE_SMALL, SIZE_LARGE);

	return 0;
}

static void __exit
destroy_safe_buffer_pools(void)
{
	if (small_buffer_pool)
		pci_pool_destroy(small_buffer_pool);
	if (large_buffer_pool)
		pci_pool_destroy(large_buffer_pool);

	small_buffer_pool = large_buffer_pool = 0;
}


/* allocate a 'safe' buffer and keep track of it */
static struct safe_buffer *
alloc_safe_buffer(void *ptr, size_t size, int direction)
{
	struct safe_buffer *buf;
	struct pci_pool *pool;
	void *safe;
	dma_addr_t safe_dma_addr;

	DPRINTK("%s(ptr=%p, size=%d, direction=%d)\n",
		__func__, ptr, size, direction);

	DO_STATS ( total_allocs++ );

	buf = kmalloc(sizeof(struct safe_buffer), GFP_ATOMIC);
	if (buf == 0) {
		printk(KERN_WARNING "%s: kmalloc failed\n", __func__);
		return 0;
	}

	if (size <= SIZE_SMALL) {
		pool = small_buffer_pool;
		safe = pci_pool_alloc(pool, GFP_ATOMIC, &safe_dma_addr);

		DO_STATS ( sbp_allocs++ );
	} else if (size <= SIZE_LARGE) {
		pool = large_buffer_pool;
		safe = pci_pool_alloc(pool, GFP_ATOMIC, &safe_dma_addr);

		DO_STATS ( lbp_allocs++ );
	} else {
		pool = 0;
		safe = pci_alloc_consistent(SA1111_FAKE_PCIDEV, size,
					    &safe_dma_addr);
	}

	if (safe == 0) {
		printk(KERN_WARNING
		       "%s: could not alloc dma memory (size=%d)\n",
		       __func__, size);
		kfree(buf);
		return 0;
	}

#ifdef STATS
	if (total_allocs % 1000 == 0)
		print_alloc_stats();
#endif

	BUG_ON(sa1111_check_dma_bug(safe_dma_addr));	// paranoia

	buf->ptr = ptr;
	buf->size = size;
	buf->direction = direction;
	buf->pool = pool;
	buf->safe = safe;
	buf->safe_dma_addr = safe_dma_addr;

	MOD_INC_USE_COUNT;
	list_add(&buf->node, &safe_buffers);

	return buf;
}

/* determine if a buffer is from our "safe" pool */
static struct safe_buffer *
find_safe_buffer(dma_addr_t safe_dma_addr)
{
	struct list_head *entry;

	list_for_each(entry, &safe_buffers) {
		struct safe_buffer *b =
			list_entry(entry, struct safe_buffer, node);

		if (b->safe_dma_addr == safe_dma_addr) {
			return b;
		}
	}

	return 0;
}

static void
free_safe_buffer(struct safe_buffer *buf)
{
	DPRINTK("%s(buf=%p)\n", __func__, buf);

	list_del(&buf->node);

	if (buf->pool)
		pci_pool_free(buf->pool, buf->safe, buf->safe_dma_addr);
	else
		pci_free_consistent(SA1111_FAKE_PCIDEV, buf->size, buf->safe,
				    buf->safe_dma_addr);
	kfree(buf);

	MOD_DEC_USE_COUNT;
}

static inline int
dma_range_is_safe(dma_addr_t addr, size_t size)
{
	unsigned int physaddr = SA1111_DMA_ADDR((unsigned int) addr);

	/* Any address within one megabyte of the start of the target
         * bank will be OK.  This is an overly conservative test:
         * other addresses can be OK depending on the dram
         * configuration.  (See sa1111.c:sa1111_check_dma_bug() * for
         * details.)
	 *
	 * We take care to ensure the entire dma region is within
	 * the safe range.
	 */

	return ((physaddr + size - 1) < (1<<20));
}

/* ************************************************** */

#ifdef STATS
static unsigned long map_op_count __initdata = 0;
static unsigned long bounce_count __initdata = 0;

static void print_map_stats(void)
{
	printk(KERN_INFO
	       "sa1111_pcibuf: map_op_count=%lu, bounce_count=%lu\n",
	       map_op_count, bounce_count);
}
#endif

static dma_addr_t
map_single(void *ptr, size_t size, int direction)
{
	dma_addr_t dma_addr;

	DO_STATS ( map_op_count++ );

	dma_addr = virt_to_bus(ptr);

	if (!dma_range_is_safe(dma_addr, size)) {
		struct safe_buffer *buf;

		DO_STATS ( bounce_count++ ) ;

		buf = alloc_safe_buffer(ptr, size, direction);
		if (buf == 0) {
			printk(KERN_ERR
			       "%s: unable to map unsafe buffer %p!\n",
			       __func__, ptr);
			return 0;
		}

		DPRINTK("%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
			__func__,
			buf->ptr, (void *) virt_to_bus(buf->ptr),
			buf->safe, (void *) buf->safe_dma_addr);

		if ((direction == PCI_DMA_TODEVICE) ||
		    (direction == PCI_DMA_BIDIRECTIONAL)) {
			DPRINTK("%s: copy out from unsafe %p, to safe %p, size %d\n",
				__func__, ptr, buf->safe, size);
			memcpy(buf->safe, ptr, size);
		}
		consistent_sync(buf->safe, size, direction);

		dma_addr = buf->safe_dma_addr;
	} else {
		consistent_sync(ptr, size, direction);
	}

#ifdef STATS
	if (map_op_count % 1000 == 0)
		print_map_stats();
#endif

	return dma_addr;
}

static void
unmap_single(dma_addr_t dma_addr, size_t size, int direction)
{
	struct safe_buffer *buf;

	buf = find_safe_buffer(dma_addr);

	if (buf) {
		BUG_ON(buf->size != size);
		BUG_ON(buf->direction != direction);

		DPRINTK("%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
			__func__,
			buf->ptr, (void *) virt_to_bus(buf->ptr),
			buf->safe, (void *) buf->safe_dma_addr);


		DO_STATS ( bounce_count++ );

		if ((direction == PCI_DMA_FROMDEVICE) ||
		    (direction == PCI_DMA_BIDIRECTIONAL)) {
			DPRINTK("%s: copy back from safe %p, to unsafe %p size %d\n",
				__func__, buf->safe, buf->ptr, size);
			memcpy(buf->ptr, buf->safe, size);
		}
		free_safe_buffer(buf);
	}
}

static void
sync_single(dma_addr_t dma_addr, size_t size, int direction)
{
	struct safe_buffer *buf;

	buf = find_safe_buffer(dma_addr);

	if (buf) {
		BUG_ON(buf->size != size);
		BUG_ON(buf->direction != direction);

		DPRINTK("%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
			__func__,
			buf->ptr, (void *) virt_to_bus(buf->ptr),
			buf->safe, (void *) buf->safe_dma_addr);

		DO_STATS ( bounce_count++ );

		switch (direction) {
		case PCI_DMA_FROMDEVICE:
			DPRINTK("%s: copy back from safe %p, to unsafe %p size %d\n",
				__func__, buf->safe, buf->ptr, size);
			memcpy(buf->ptr, buf->safe, size);
			break;
		case PCI_DMA_TODEVICE:
			DPRINTK("%s: copy out from unsafe %p, to safe %p, size %d\n",
				__func__,buf->ptr, buf->safe, size);
			memcpy(buf->safe, buf->ptr, size);
			break;
		case PCI_DMA_BIDIRECTIONAL:
			BUG();	/* is this allowed?  what does it mean? */
		default:
			BUG();
		}
		consistent_sync(buf->safe, size, direction);
	} else {
		consistent_sync(bus_to_virt(dma_addr), size, direction);
	}
}

/* ************************************************** */

/*
 * see if a buffer address is in an 'unsafe' range.  if it is
 * allocate a 'safe' buffer and copy the unsafe buffer into it.
 * substitute the safe buffer for the unsafe one.
 * (basically move the buffer from an unsafe area to a safe one)
 */
dma_addr_t
sa1111_map_single(void *ptr, size_t size, int direction)
{
	unsigned long flags;
	dma_addr_t dma_addr;

	DPRINTK("%s(ptr=%p,size=%d,dir=%x)\n",
	       __func__, ptr, size, direction);

	BUG_ON(direction == PCI_DMA_NONE);

	local_irq_save(flags);

	dma_addr = map_single(ptr, size, direction);

	local_irq_restore(flags);

	return dma_addr;
}

/*
 * see if a mapped address was really a "safe" buffer and if so, copy
 * the data from the safe buffer back to the unsafe buffer and free up
 * the safe buffer.  (basically return things back to the way they
 * should be)
 */

void
sa1111_unmap_single(dma_addr_t dma_addr, size_t size, int direction)
{
	unsigned long flags;

	DPRINTK("%s(ptr=%p,size=%d,dir=%x)\n",
		__func__, (void *) dma_addr, size, direction);

	BUG_ON(direction == PCI_DMA_NONE);

	local_irq_save(flags);

	unmap_single(dma_addr, size, direction);

	local_irq_restore(flags);
}

int
sa1111_map_sg(struct scatterlist *sg, int nents, int direction)
{
	unsigned long flags;
	int i;

	DPRINTK("%s(sg=%p,nents=%d,dir=%x)\n",
		__func__, sg, nents, direction);

	BUG_ON(direction == PCI_DMA_NONE);

	local_irq_save(flags);

	for (i = 0; i < nents; i++, sg++) {
		struct page *page = sg->page;
		unsigned int offset = sg->offset;
		unsigned int length = sg->length;
		void *ptr = page_address(page) + offset;

		sg->dma_address =
			map_single(ptr, length, direction);
	}

	local_irq_restore(flags);

	return nents;
}

void
sa1111_unmap_sg(struct scatterlist *sg, int nents, int direction)
{
	unsigned long flags;
	int i;

	DPRINTK("%s(sg=%p,nents=%d,dir=%x)\n",
		__func__, sg, nents, direction);

	BUG_ON(direction == PCI_DMA_NONE);

	local_irq_save(flags);

	for (i = 0; i < nents; i++, sg++) {
		dma_addr_t dma_addr = sg->dma_address;
		unsigned int length = sg->length;

		unmap_single(dma_addr, length, direction);
	}

	local_irq_restore(flags);
}

void
sa1111_dma_sync_single(dma_addr_t dma_addr, size_t size, int direction)
{
	unsigned long flags;

	DPRINTK("%s(ptr=%p,size=%d,dir=%x)\n",
		__func__, (void *) dma_addr, size, direction);

	local_irq_save(flags);

	sync_single(dma_addr, size, direction);

	local_irq_restore(flags);
}

void
sa1111_dma_sync_sg(struct scatterlist *sg, int nents, int direction)
{
	unsigned long flags;
	int i;

	DPRINTK("%s(sg=%p,nents=%d,dir=%x)\n",
		__func__, sg, nents, direction);

	BUG_ON(direction == PCI_DMA_NONE);

	local_irq_save(flags);

	for (i = 0; i < nents; i++, sg++) {
		dma_addr_t dma_addr = sg->dma_address;
		unsigned int length = sg->length;

		sync_single(dma_addr, length, direction);
	}

	local_irq_restore(flags);
}

EXPORT_SYMBOL(sa1111_map_single);
EXPORT_SYMBOL(sa1111_unmap_single);
EXPORT_SYMBOL(sa1111_map_sg);
EXPORT_SYMBOL(sa1111_unmap_sg);
EXPORT_SYMBOL(sa1111_dma_sync_single);
EXPORT_SYMBOL(sa1111_dma_sync_sg);

/* **************************************** */

static int __init sa1111_pcibuf_init(void)
{
	int ret;

	printk(KERN_DEBUG
	       "sa1111_pcibuf: initializing SA-1111 DMA workaround\n");

	ret = create_safe_buffer_pools();

	return ret;
}
module_init(sa1111_pcibuf_init);

static void __exit sa1111_pcibuf_exit(void)
{
	BUG_ON(!list_empty(&safe_buffers));

#ifdef STATS
	print_alloc_stats();
	print_map_stats();
#endif

	destroy_safe_buffer_pools();
}
module_exit(sa1111_pcibuf_exit);

MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>");
MODULE_DESCRIPTION("Special pci_{map/unmap/dma_sync}_* routines for SA-1111.");
MODULE_LICENSE("GPL");