Skip to content

Commit 72b4bcb

Browse files
committed
ALSA: docs: Update for managed buffer allocation mode
Update the documentation for the newly introduced managed buffer allocation mode accordingly. The old preallocation is no longer recommended. Link: https://lore.kernel.org/r/20191117085308.23915-3-tiwai@suse.de Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 0dba808 commit 72b4bcb

1 file changed

Lines changed: 64 additions & 35 deletions

File tree

‎Documentation/sound/kernel-api/writing-an-alsa-driver.rst‎

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,21 +1270,23 @@ shows only the skeleton, how to build up the PCM interfaces.
12701270
/* the hardware-specific codes will be here */
12711271
....
12721272
return 0;
1273-
12741273
}
12751274

12761275
/* hw_params callback */
12771276
static int snd_mychip_pcm_hw_params(struct snd_pcm_substream *substream,
12781277
struct snd_pcm_hw_params *hw_params)
12791278
{
1280-
return snd_pcm_lib_malloc_pages(substream,
1281-
params_buffer_bytes(hw_params));
1279+
/* the hardware-specific codes will be here */
1280+
....
1281+
return 0;
12821282
}
12831283

12841284
/* hw_free callback */
12851285
static int snd_mychip_pcm_hw_free(struct snd_pcm_substream *substream)
12861286
{
1287-
return snd_pcm_lib_free_pages(substream);
1287+
/* the hardware-specific codes will be here */
1288+
....
1289+
return 0;
12881290
}
12891291

12901292
/* prepare callback */
@@ -1382,9 +1384,9 @@ shows only the skeleton, how to build up the PCM interfaces.
13821384
&snd_mychip_capture_ops);
13831385
/* pre-allocation of buffers */
13841386
/* NOTE: this may fail */
1385-
snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1386-
&chip->pci->dev,
1387-
64*1024, 64*1024);
1387+
snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1388+
&chip->pci->dev,
1389+
64*1024, 64*1024);
13881390
return 0;
13891391
}
13901392

@@ -1465,13 +1467,14 @@ The operators are defined typically like this:
14651467
All the callbacks are described in the Operators_ subsection.
14661468

14671469
After setting the operators, you probably will want to pre-allocate the
1468-
buffer. For the pre-allocation, simply call the following:
1470+
buffer and set up the managed allocation mode.
1471+
For that, simply call the following:
14691472

14701473
::
14711474

1472-
snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1473-
&chip->pci->dev,
1474-
64*1024, 64*1024);
1475+
snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1476+
&chip->pci->dev,
1477+
64*1024, 64*1024);
14751478

14761479
It will allocate a buffer up to 64kB as default. Buffer management
14771480
details will be described in the later section `Buffer and Memory
@@ -1621,8 +1624,7 @@ For the operators (callbacks) of each sound driver, most of these
16211624
records are supposed to be read-only. Only the PCM middle-layer changes
16221625
/ updates them. The exceptions are the hardware description (hw) DMA
16231626
buffer information and the private data. Besides, if you use the
1624-
standard buffer allocation method via
1625-
:c:func:`snd_pcm_lib_malloc_pages()`, you don't need to set the
1627+
standard managed buffer allocation mode, you don't need to set the
16261628
DMA buffer information by yourself.
16271629

16281630
In the sections below, important records are explained.
@@ -1776,8 +1778,8 @@ the physical address of the buffer. This field is specified only when
17761778
the buffer is a linear buffer. ``dma_bytes`` holds the size of buffer
17771779
in bytes. ``dma_private`` is used for the ALSA DMA allocator.
17781780

1779-
If you use a standard ALSA function,
1780-
:c:func:`snd_pcm_lib_malloc_pages()`, for allocating the buffer,
1781+
If you use either the managed buffer allocation mode or the standard
1782+
API function :c:func:`snd_pcm_lib_malloc_pages()` for allocating the buffer,
17811783
these fields are set by the ALSA middle layer, and you should *not*
17821784
change them by yourself. You can read them but not write them. On the
17831785
other hand, if you want to allocate the buffer by yourself, you'll
@@ -1929,8 +1931,12 @@ Many hardware setups should be done in this callback, including the
19291931
allocation of buffers.
19301932

19311933
Parameters to be initialized are retrieved by
1932-
:c:func:`params_xxx()` macros. To allocate buffer, you can call a
1933-
helper function,
1934+
:c:func:`params_xxx()` macros.
1935+
1936+
When you set up the managed buffer allocation mode for the substream,
1937+
a buffer is already allocated before this callback gets
1938+
called. Alternatively, you can call a helper function below for
1939+
allocating the buffer, too.
19341940

19351941
::
19361942

@@ -1964,18 +1970,23 @@ hw_free callback
19641970
static int snd_xxx_hw_free(struct snd_pcm_substream *substream);
19651971

19661972
This is called to release the resources allocated via
1967-
``hw_params``. For example, releasing the buffer via
1968-
:c:func:`snd_pcm_lib_malloc_pages()` is done by calling the
1969-
following:
1970-
1971-
::
1972-
1973-
snd_pcm_lib_free_pages(substream);
1973+
``hw_params``.
19741974

19751975
This function is always called before the close callback is called.
19761976
Also, the callback may be called multiple times, too. Keep track
19771977
whether the resource was already released.
19781978

1979+
When you have set up the managed buffer allocation mode for the PCM
1980+
substream, the allocated PCM buffer will be automatically released
1981+
after this callback gets called. Otherwise you'll have to release the
1982+
buffer manually. Typically, when the buffer was allocated from the
1983+
pre-allocated pool, you can use the standard API function
1984+
:c:func:`snd_pcm_lib_malloc_pages()` like:
1985+
1986+
::
1987+
1988+
snd_pcm_lib_free_pages(substream);
1989+
19791990
prepare callback
19801991
~~~~~~~~~~~~~~~~
19811992

@@ -3543,6 +3554,25 @@ Once the buffer is pre-allocated, you can use the allocator in the
35433554

35443555
Note that you have to pre-allocate to use this function.
35453556

3557+
Most of drivers use, though, rather the newly introduced "managed
3558+
buffer allocation mode" instead of the manual allocation or release.
3559+
This is done by calling :c:func:`snd_pcm_set_managed_buffer_all()`
3560+
instead of :c:func:`snd_pcm_lib_preallocate_pages_for_all()`.
3561+
3562+
::
3563+
3564+
snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
3565+
&pci->dev, size, max);
3566+
3567+
where passed arguments are identical in both functions.
3568+
The difference in the managed mode is that PCM core will call
3569+
:c:func:`snd_pcm_lib_malloc_pages()` internally already before calling
3570+
the PCM ``hw_params`` callback, and call :c:func:`snd_pcm_lib_free_pages()`
3571+
after the PCM ``hw_free`` callback automatically. So the driver
3572+
doesn't have to call these functions explicitly in its callback any
3573+
longer. This made many driver code having NULL ``hw_params`` and
3574+
``hw_free`` entries.
3575+
35463576
External Hardware Buffers
35473577
-------------------------
35483578

@@ -3697,17 +3727,17 @@ provides an interface for handling SG-buffers. The API is provided in
36973727
``<sound/pcm.h>``.
36983728

36993729
For creating the SG-buffer handler, call
3700-
:c:func:`snd_pcm_lib_preallocate_pages()` or
3701-
:c:func:`snd_pcm_lib_preallocate_pages_for_all()` with
3730+
:c:func:`snd_pcm_set_managed_buffer()` or
3731+
:c:func:`snd_pcm_set_managed_buffer_all()` with
37023732
``SNDRV_DMA_TYPE_DEV_SG`` in the PCM constructor like other PCI
37033733
pre-allocator. You need to pass ``&pci->dev``, where pci is
37043734
the :c:type:`struct pci_dev <pci_dev>` pointer of the chip as
37053735
well.
37063736

37073737
::
37083738

3709-
snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
3710-
&pci->dev, size, max);
3739+
snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
3740+
&pci->dev, size, max);
37113741

37123742
The ``struct snd_sg_buf`` instance is created as
37133743
``substream->dma_private`` in turn. You can cast the pointer like:
@@ -3716,8 +3746,7 @@ The ``struct snd_sg_buf`` instance is created as
37163746

37173747
struct snd_sg_buf *sgbuf = (struct snd_sg_buf *)substream->dma_private;
37183748

3719-
Then call :c:func:`snd_pcm_lib_malloc_pages()` in the ``hw_params``
3720-
callback as well as in the case of normal PCI buffer. The SG-buffer
3749+
Then in :c:func:`snd_pcm_lib_malloc_pages()` call, the common SG-buffer
37213750
handler will allocate the non-contiguous kernel pages of the given size
37223751
and map them onto the virtually contiguous memory. The virtual pointer
37233752
is addressed in runtime->dma_area. The physical address
@@ -3726,8 +3755,8 @@ physically non-contiguous. The physical address table is set up in
37263755
``sgbuf->table``. You can get the physical address at a certain offset
37273756
via :c:func:`snd_pcm_sgbuf_get_addr()`.
37283757

3729-
To release the data, call :c:func:`snd_pcm_lib_free_pages()` in
3730-
the ``hw_free`` callback as usual.
3758+
If you need to release the SG-buffer data explicitly, call the
3759+
standard API function :c:func:`snd_pcm_lib_free_pages()` as usual.
37313760

37323761
Vmalloc'ed Buffers
37333762
------------------
@@ -3740,8 +3769,8 @@ buffer preallocation with ``SNDRV_DMA_TYPE_VMALLOC`` type.
37403769

37413770
::
37423771

3743-
snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_VMALLOC,
3744-
NULL, 0, 0);
3772+
snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC,
3773+
NULL, 0, 0);
37453774

37463775
The NULL is passed to the device pointer argument, which indicates
37473776
that the default pages (GFP_KERNEL and GFP_HIGHMEM) will be
@@ -3758,7 +3787,7 @@ argument.
37583787

37593788
::
37603789

3761-
snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_VMALLOC,
3790+
snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC,
37623791
snd_dma_continuous_data(GFP_KERNEL | __GFP_DMA32), 0, 0);
37633792

37643793
Proc Interface

0 commit comments

Comments
 (0)